Reputation: 241
I have not seen this in MATLAB. Suppose I have
V1 = [1 0], V2 = [0 1]
I want to create a matrix such that the matrix has to be
[[1 0] [0 1]
[0 1] [0 2]]
i.e., the first row and column are [V1 V2]
, element (2,2)
of the matrix is [0 1]+[0 1]=[0 2]
So index (1,1)
of the matrix should be [1 0]
, index (1,2)
of matrix should be [0 1]
.
Is there any way I can implement this in MATLAB?
Upvotes: 0
Views: 434
Reputation: 30047
A matrix can only contain one value per element. Here are 3 options for how you could get close to what you've described:
You could create a 4*4 matrix like so:
V1 = [1 0]; V2 = [0 1];
M = [V1, V2; V2, 2*V2];
And then create a shorthand function to index this in blocks of 2
Mb = @(r,c) M( r, 2*c-1+[0,1] );
Mb(1,1); % = [1 0]
Mb(1,2); % = [0 1]
Mb(2,2); % = [0 2]
Note that this doesn't work for assignment back to M
in blocks of 2, just for reading the value.
Alternatively, you could use a cell array
C = {V1, V2; V2, 2*V2};
Now you can index this how you want, but it won't behave as a single matrix, and you can't do numerical operations on the whole cell array
C{2,2}; % = [2, 2], note the curly brace indexing
A third option would be to make your matrix 3D
V1 = reshape( V1, 1, 1, [] );
V2 = reshape( V2, 1, 1, [] );
M3D = [V1, V2; V2, 2*V2];
Now you can index in the 3rd dimension
M3D(2,2,:); % = [0 2], but size [1,1,2]. Could reshape(M3D(2,2,:),1,[]) to get a row.
Upvotes: 3