2

as i said in topic name, i want to put variables of 3 arrays in a row of another array.
look: for example i have 3 arrays X1, X2, X3 that them variables are:

X1=[1 2 3];   
X2=[4 5 6];   
X3=[7 8 9];  

and another array Y is this look:

Y=zeros(3,3);   
0  0  0   
0  0  0   
0  0  0   

now i want randomize X1 in first row, X2 in second row and X3 in third row like this:

3  1  2   
4  6  5   
9  8  7  

many thanx :)

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147

2 Answers2

1

Use randperm:

n = size(Y,2); %// number of columns
Y(1, randperm(n)) = X1;
Y(2, randperm(n)) = X2;
Y(3, randperm(n)) = X3;
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
  • it is correct thanx. im amateur in Matlab please explain 'size(Y,2)' and also randperm. – Sajad Khammar May 08 '15 at 17:25
  • `size(Y,2)` is the number of columns of `Y` (more info about `size` function [here](http://es.mathworks.com/help/matlab/ref/size.html)). `randperm(n)` is a random permutation of the numbers `1`, `2` , ..., `n`. See the [link in my answer](http://es.mathworks.com/help/matlab/ref/randperm.html) for more details about `randperm`. – Luis Mendo May 08 '15 at 17:34
  • i have another question. X1=[1 2 3]; ==> the hour of start of exam X2=[1...n]; ===> the day start program X3=[1..n]; ===> these are courses code for each number in X2(day) we should have each number of X1. X1 1 2 3 1 2 3 ... X2 1 1 1 2 2 3 ... X3 no matter have i spoke clearly? :-s – Sajad Khammar May 08 '15 at 17:50
1

This is easier to do if your Xi row vectors are in a single array X.

EDIT: Thanks to LuisMendo for the optimization suggestion.

X = [X1;X2;X3];
[rows,cols] = size(X);
Y = zeros(rows,cols);

for i = 1:rows
    Y(i,randperm(cols)) = X(i,:);
end
eigenchris
  • 5,791
  • 2
  • 21
  • 30
  • 1
    It would be interesting to know which is faster: `Y(i,:) = x(randperm(size(X,2)))` or `Y(i,randperm(size(X,2))) = x` – Luis Mendo May 08 '15 at 17:37
  • @LuisMendo Nice thinking. That's probably the faster method. – eigenchris May 08 '15 at 17:42
  • i have another question. X1=[1 2 3]; ==> the hour of start of exam X2=[1...n]; ===> the day start program X3=[1..n]; ===> these are courses code for each number in X2(day) we should have each number of X1. X1 1 2 3 1 2 3 ... X2 1 1 1 2 2 3 ... X3 no matter have i spoke clearly? :-s – Sajad Khammar May 08 '15 at 17:49
  • @SajadKhammar In other words, you want to pick one element from `X1`, one element from `X2`, and one element from `X3`, and find all possible combinations? – eigenchris May 08 '15 at 17:59
  • @eigenchris at first, i chose your coding because when i have expanded my arrays its was ok. ************* i am trying to build a simple coding for examination program planning. i have hour and day and course code. all of these show with digits. for example hour '1' is 8am to 10am or hour '2' is 11 am to 13pm and hour '3' may be 15pm to 17pm . and days are 1 to n (holding range). and third is courses code no matter witch one. eg. Math is '1' ... now i want build a program that one course do not start in a day and also in same day. wish you got my bad English :( :'( :s – Sajad Khammar May 08 '15 at 18:14
  • @eigenchris I didn't mean to imply that indexing on the left-hand side is faster than on the right-hand side. I was only suggesting that one of the options may be faster than the other, but I'm not sure which one. In other words: I'm not sure whether your edit is really an _optimization_ :-) – Luis Mendo May 08 '15 at 18:17
  • 1
    @LuisMendo The suggestion you made does seem to be mildly faster based on some tests I did. More time was saved by combining the two lines in my loop into one, however. – eigenchris May 08 '15 at 18:22
  • @eigenchris do you got my question? – Sajad Khammar May 08 '15 at 18:31
  • @eigenchris Oh, so you tested both; got it. And yes, that intermediate `x` seemed unnecessary. Maybe also change `i` into some other name, to [avoid shadowing](http://stackoverflow.com/questions/14790740/using-i-and-j-as-variables-in-matlab)? – Luis Mendo May 08 '15 at 18:32
  • @LuisMendo yes, it appears that in general indexing on the left increases the speed of assignment by a marginal amount, though I am not sure why. Maybe because indexing on the right creates a whole new object in memory that must then be assigned? I'm not sure how MATLAB handles this at a low level. As for the `i` issue, I tend to use the `1i` solution when I need the imaginary unit, and freely use `i` and `j` as loop variables, though that's just my personal preference. – eigenchris May 08 '15 at 18:39
  • @SajadKhammar So you want to find all combinations of hours, days, and course codes, so that courses do not repeat themselves in the same day? – eigenchris May 08 '15 at 18:42
  • @SajadKhammar That's a significant change to your original question. Maybe post a new one? – Luis Mendo May 08 '15 at 18:59
  • @LuisMendo please check here: http://stackoverflow.com/questions/30095855/examation-program-planning-using-matlab – Sajad Khammar May 08 '15 at 19:16
  • @eigenchris please check here: http://stackoverflow.com/questions/30095855/examation-program-planning-using-matlab – Sajad Khammar May 08 '15 at 19:17
  • @LuisMendo im waiting for your help friend – Sajad Khammar May 08 '15 at 19:43