Why is this ending my matrices before they are filled, and how do I print the answer of the multiplication?
조회 수: 4 (최근 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!