matrix operatios to convert two vectors into a matrix

1 Ansicht (letzte 30 Tage)
Abirami
Abirami am 20 Aug. 2014
Kommentiert: Adam am 20 Aug. 2014
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 Kommentare
Matt J
Matt J am 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 am 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Adam
Adam am 20 Aug. 2014
Bearbeitet: Adam am 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.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by