Abirami Anbalagan
Abirami Anbalagan

Reputation: 196

matrix operation where the values can be expressed as a set

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...

Upvotes: 1

Views: 77

Answers (2)

bla
bla

Reputation: 26069

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

Upvotes: 2

Luis Mendo
Luis Mendo

Reputation: 112659

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.

Upvotes: 1

Related Questions