MATLAB Answers

How do you append to a matrix within a for loop when the matrices are unequal in size?

1 view (last 30 days)
Krish Gupta
Krish Gupta on 29 Aug 2019
Commented: Steven Lord on 29 Aug 2019
N=input("How many matrices you want to enter?")
D1=[];
for a=1:N
m=input(' give the no. of columns:')
n=input(' give the no. of rows:')
for i=1:n
for j=1:m
C(i,j)=input("Give a number:")
end
end
D1=[D1;C]
end
% I want to get the two matrices, as input,[1 2;3 4] and [5 7 8; 6 9 10] stored in another array
%MY program above takes the input for the two matrices
% I want to store those two input matrices in some data type
% Suggest me how to do it
% the last line for D1 shows error for uneuqual sized matrices while it works for equal sized matrices

Accepted Answer

James Tursa
James Tursa on 29 Aug 2019
Maybe you could use a cell array. E.g.,
:
D1 = cell(1,N);
for a=1:N
:
D1{a} = C;
end
Then the first matrix is D1{1}, the second matrix is D1{2}, etc.

  2 Comments

Steven Lord
Steven Lord on 29 Aug 2019
FYI, I deleted Krish Gupta's flag indicating that the answer worked well.
Please use flags for indicating that a comment or answer is unclear, not appropriate, or is spam. Use comments to offer thanks, to make follow-up requests on the same issue, or to provide additional information (such as when someone asks for clarification.) Thanks.

Sign in to comment.

More Answers (0)

Sign in to answer this question.