MATLAB Answers

0

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

Asked by Krish Gupta on 29 Aug 2019
Latest activity Commented on by 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

  1 Comment

Is there any other methods at taking input for matrices?

Sign in to comment.

1 Answer

Answer by James Tursa
on 29 Aug 2019
 Accepted Answer

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

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.