Why is this ending my matrices before they are filled, and how do I print the answer of the multiplication?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Steven Lord
2021-1-31
%{
P=zeros(R:C);
%}
This doesn't do what you want.
R = 5;
C = 2;
P1 = zeros(R:C)
P2 = zeros(R,C)
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)
P = zeros(R:C)
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.
回答(1 个)
Walter Roberson
2021-1-30
编辑:Walter Roberson
2021-1-30
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 个评论
Stephen23
2021-1-31
编辑:Stephen23
2021-1-31
"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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!