Why is this ending my matrices before they are filled, and how do I print the answer of the multiplication?

1 view (last 30 days)
C=input('How many columns are there in P? ');
R=input('How many rows are there in P? ');
P=zeros(R:C);
co=1;
ro=1;
while co<=C+1 && ro<=R+1;
P(co)=input('What is the first value of this column of P? ')
co=co+1;
P(co)=input('What is the next value of this column of P? ')
co=co+1;
ro=ro+1;
end
B=input('How many columns are there in D? ');
T=input('How many rows are there in D? ');
D=zeros(R:C);
co=1;
ro=1;
while co<=C+1 && ro<=R+1;
D(co)=input('What is the first value of this column of D? ')
co=co+1;
D(co)=input('What is the next value of this column of D? ')
co=co+1;
ro=ro+1;
end
V = P*D;
Print(V)
When entering the values of the matrices if I make it more than 2 columns it puts zeros in for the 3rd and subsequent columns. I'm also trying to print (not sure what the command is in matlab) V, to have the value show. However using print it searches my hard drive to try and find a file (which is odd since I'm not using open).
I would assume if the matrices aren't able to be multiplied and error message shows up, but is there a way to make that a dialogue box?
  1 Comment
Steven Lord
Steven Lord on 31 Jan 2021
%{
P=zeros(R:C);
%}
This doesn't do what you want.
R = 5;
C = 2;
P1 = zeros(R:C)
P1 = []
P2 = zeros(R,C)
P2 = 5×2
0 0 0 0 0 0 0 0 0 0
The expression 5:2 returns the empty array. When you pass that into zeros as the size input you get a 0-by-0. But even worse, if C was much larger than R at best MATLAB would tell you you can't make that large an array. At worst it would try and potentially take a long time to allocate.
R = 2;
C = 30;
numElements = prod(R:C)
numElements = 2.6525e+32
P = zeros(R:C)
Error using zeros
Requested array exceeds the maximum possible variable size.
You can't make a 2-by-3-by-4-by-5-by-6-by-...-by-29-by-30 array because it would have over 2e32 elements.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 30 Jan 2021
Edited: Walter Roberson on 30 Jan 2021
print() of a value is Python. MATLAB uses disp()
To make a dialog box for that error:
try
V = P*D;
catch ME
errordlg(getReport(ME)) ;
end
  3 Comments
David Scidmore
David Scidmore on 31 Jan 2021
Got it,
My intent is to let users form what ever size matrix they wish and fill it with their own values. I thought the zeros would clear out the matrix to be filled.
Like they want P to be a 3*5 Matrix they enter the values they want and T could be a 5*3 matrix and they enter the values they wish. Not to be a zero matrix.
My new code only puts everything in a single row.
C=input('How many columns are there in P? ');
R=input('How many rows are there in P? ');
P=(R:C);
co=1;
ro=1;
while co<=C+1 && ro<=R+1;
P(co)=input('What is the first value of this column of P? ')
co=co+1;
P(co)=input('What is the next value of this column of P? ')
ro=ro+1;
ro=ro+1;
end
Stephen23
Stephen23 on 31 Jan 2021
Edited: Stephen23 on 31 Jan 2021
"I thought the zeros would clear out the matrix to be filled."
Sure, it would.
But you are missing the difference between
zeros(A:B) % what you did (colon does not give the required input to ZEROS)
and
zeros(A,B) % what you need (comma defines two separate inputs)
"My new code only puts everything in a single row."
Because that is what you told MATLAB to do:
P = (R:C) % output is always a row vector

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!