random presentation of the columns of a matrix

1 Ansicht (letzte 30 Tage)
Maria K
Maria K am 12 Okt. 2017
Kommentiert: Maria K am 12 Okt. 2017
Hello everyone! I am really new on matlab and I need to know how I can shuffle the columns of a matrix without changing the order of elements in every row. My code is this:
trialType = [1 2 3];
Examples = [4 5 6];
% Make a condition matrix
a = [trialType(1) Examples(1)]; %zeugos x & 1 fora
b = [trialType(1) Examples(2)]; %zeugos x & 2 fores
c = [trialType(1) Examples(3)]; %zeugos x & 3 fores
d = [trialType(2) Examples(1)]; %zeugos y & 1 fora
e = [trialType(2) Examples(2)]; %zeugos y & 2 fores
f = [trialType(2) Examples(3)]; %zeugos y & 3 fores
g = [trialType(3) Examples(1)]; %zeugos both & 1 fora
h = [trialType(3) Examples(2)]; %zeugos both & 2 fores
i = [trialType(3) Examples(3)]; %zeugos both & 3 fores
condMat = [a; b; c; d; e; f; g; h; i];
So disp(condMat) will give me:
1 4
1 5
1 6
2 4
2 5
2 6
3 4
3 5
I want something like this:
1 6
3 4
1 4
etc.
Thanks on advance for your replies :)

Akzeptierte Antwort

Guillaume
Guillaume am 12 Okt. 2017
First, a simpler way to generate your matrix:
trialType = [1 2 3];
Examples = [4 5 6];
[c1, c2] = ndgrid(trialType, Examples);
condMat = [c1(:), c2(:)];
Then, to randomly permute the rows of condMat, use randperm to generate a randomly ordered list of row indices:
condMat = condMat(randperm(size(condMat, 1)), :)

Weitere Antworten (2)

KSSV
KSSV am 12 Okt. 2017
trialType = [1 2 3];
Examples = [4 5 6];
N = 10 ;
iwant = zeros(N,2) ;
for i = 1:N
iwant(i,:) = [randsample(trialType,1) randsample(Examples,1)] ;
end
iwant
  1 Kommentar
Maria K
Maria K am 12 Okt. 2017
Bearbeitet: Maria K am 12 Okt. 2017
Hmm maybe I didn't express myself correctly.. I want something like that:
2 4
2 5
1 4
2 6
3 4
1 5
3 5
1 6
3 6
so we should get 1 with 4, 1 with 5, 1 with 6, 2 with 4, 2 with 5, 2 with 6, 3 with 4, 3 with 5, 3 with 6, but the row of every combination (e.g. 1 with 4) should be random...
Thanks for your efford :)

Melden Sie sich an, um zu kommentieren.


Matt J
Matt J am 12 Okt. 2017
[m,n]=size(condmat);
condMat = condMat(randperm(m),:);

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by