Why is this ending my matrices before they are filled, and how do I print the answer of the multiplication?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentar
Steven Lord
am 31 Jan. 2021
%{
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.
Antworten (1)
Walter Roberson
am 30 Jan. 2021
Bearbeitet: Walter Roberson
am 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 Kommentare
Stephen23
am 31 Jan. 2021
Bearbeitet: Stephen23
am 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
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!