1
votes

Is it possible to obtain a matrix as follows??

The input vectors are X(column vector) and Y(row vector)

 X=[2                       Y=[5 3 1 2 4]-1*5 vector
    4
    5
    3
    1]-5*1 vector

both vectors have the index values as elements. Now I want to have a 5*5 matrix which is as follows:

 Z= (2,5) (2,3) (2,1) (2,2) (2,4)
    (4,5) (4,3) (4,1) (4,2) (4,4)
    (5,5) (5,3) (5,1) (5,2) (5,4)
    (3,5) (3,3) (3,1) (3,2) (3,4)
    (1,5) (1,3) (1,1) (1,2) (1,4)

Z-5*5 matrix

is it possible to obtain a matrix like this using matlab...pls help....i have no idea how to do this....thanks in advance...

2
Use your favourite search engine on the term Matlab vector outer product and variations thereof.High Performance Mark
What do you mean by (2,5)? It's not valid Matlab syntaxLuis Mendo
actually i want to scramble a matrix so i generated two chaotic sequences and i sorted them...i need to use the index values of these two sequences to scramble the original atrix...so i thought i could convert these two vectors into a matrix so that id be able to shuffle the original matrix values into the new positions...Abirami Anbalagan

2 Answers

2
votes

Here's an alternative solution using a cell array instead of a regular array:

XX=meshgrid(X);
YY=meshgrid(Y);
C=reshape(num2cell([XX(:) YY(:)],2),numel(X),[]);

The outcome will be a 5x5 cell array,

C = 
[1x2 double]    [1x2 double]    [1x2 double]    [1x2 double]    [1x2 double]
[1x2 double]    [1x2 double]    [1x2 double]    [1x2 double]    [1x2 double]
[1x2 double]    [1x2 double]    [1x2 double]    [1x2 double]    [1x2 double]
[1x2 double]    [1x2 double]    [1x2 double]    [1x2 double]    [1x2 double]
[1x2 double]    [1x2 double]    [1x2 double]    [1x2 double]    [1x2 double]

each element will contain the 2 numbers. For example:

 C{2,2}
 ans =  
     4     3
1
votes

Maybe this is what you want:

Z = cat(3, repmat(X, 1, size(Y,2)), repmat(Y, size(X,1), 1));

This builds a 3D-array Z such that Z(m,n,:) gives the m,n entry of your "matrix".

However, depending on what you want to achieve, there are probably better ways to do it.