matrix operatios to convert two vectors into a matrix

1 view (last 30 days)
Hello, is it possible to obtain a matrix as follows. X and Y are the input vectors required to change into a matrix..
X=[2 Y=[5 3 1 2 4]-1*5 vector
4
5
3
1]-5*1 vector
both the 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 Comments
Matt J
Matt J on 20 Aug 2014
Your Z is not a matrix because its entries are not scalars. There is no MATLAB data type that will efficiently hold large arrays of non-scalar data. You should look for an approach which doesn't require X and Y to be combined into a single array.
Adam
Adam on 20 Aug 2014
Despite my potential solution below I would agree with Matt J on the whole though on considering whether you really need this in one matrix.
Possibly you could simply work with the intermediate meshgrid results.
Cell arrays are good for allowing you to store arbitrary things in a matrix form, but their efficiency is not great if you need speed and large amounts of data.

Sign in to comment.

Accepted Answer

Adam
Adam on 20 Aug 2014
Edited: Adam on 20 Aug 2014
[Ygrid, Xgrid] = meshgrid( Y, X );
Z = arrayfun( @(X,Y) [X Y], Xgrid, Ygrid, 'UniformOutput', false );
should give you a 5*5 cell array where each cell is the pair you describe.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!