Reputation: 33
I am not able to save the value of BB
in Bv
.
MATLAB returns this error:
Subscripted assignment dimension mismatch.
Please help me to do it.
X=[1 6 9 5; 6 36 54 30; 9 54 81 40; 5 30 40 25]
[N1,dim1]=size(X) ;
for i=1:N1
bb=X(i:end,1)*X(i,i:end);
BB=bb(triu(true(size(bb))))
Bv(i,:)=BB(:);
end
Upvotes: 2
Views: 128
Reputation: 18177
As @Rashid suggests, use cell arrays instead of numeric arrays. The beauty of cell arrays is that it can store matrices of different type and size in 1 storage unit. It is much like a structure, but with indices to easily call entries.
X=[1 6 9 5; 6 36 54 30; 9 54 81 40; 5 30 40 25];
for ii=1:size(x,1)
bb=X(ii:end,1)*X(ii,ii:end);
BB=bb(triu(true(size(bb))))
Bv{ii,:}=BB(:);
end
Note that I also changed your loop index to use ii
as opposed to i
, see here. i
is the imaginary unit and to prevent errors it's better to not overwrite build-in functions.
Just an example of how a cell array stores different data types and sizes:
A = magic(2); % 2x2 double
B = uint8(magic(3)); % 3x3 uint8
C = 'hello world'; % string
YourCell{1} = A;
YourCell{2} = B;
YourCell{3} = C;
YourCell =
[2x2 double] [3x3 uint8] 'hello world'
The same but now as a structure:
YourStruct.magic2double = A;
YourStruct.magic3uint8 = B;
YourStruct.MyString = C;
YourStruct =
magic2double: [2x2 double]
magic3uint8: [3x3 uint8]
MyString: 'hello world'
The cell and structure contain the same information, but for information in the cell you call YourCell{ii}
, whilst in the structure you must call YourStruct.variablename
. The cell can be accessed by indexing, the structure cannot. For the structure however you can use easy names to remember what you stored in each element, whilst that's impossible for the cell.
Upvotes: 2