Info
Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.
cannot run an if statement with "or" and "and" operators in it. I get an error saying "matrix dimensions must agree". I added a comment in a line where the error occurs.
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
number=4391
currentNum=number
numvec=zeros(4,1)
for b=1:4
currentRem=mod(currentNum,10);
numvec(b)=currentRem
currentNum=(currentNum-currentRem)/10
end
empty=zeros(4,1);
newnumvec=numvec
maximum=zeros(3,1);
for n=1:4
for s=1:4
if (newnumvec(1)>newnumvec(2:4)) | (newnumvec(n)>newnumvec((n+1):end) & newnumvec(n)>newnumvec(1:(n-1))) | (newnumvec(4)>newnumvec(1:3)) %here the error occurs
maximum(s)=newnumvec(n)
empty(n)=newnumvec(n)
newnumvec=newnumvec-empty
end %if
end %for
end %for
0 Kommentare
Antworten (2)
KALYAN ACHARJYA
am 16 Dez. 2019
You are trying to compare two differnt data types-
Here
if (newnumvec(1)>newnumvec(2:4)) |....
%what result ^ expected from this statement
end
#See, you trying to compare 1>[9 3 4]..??
>> newnumvec(1)
ans =
1
>> newnumvec(2:4)
ans =
9
3
4
Same for others also.
2 Kommentare
KALYAN ACHARJYA
am 17 Dez. 2019
Here newnumvec have 4
>> whos newnumvec
Name Size Bytes Class Attributes
newnumvec 4x1 32 double
But you trying to acesss n+1 in if statement
newnumvec((n+1):end)) % For loop when n=1, then newnumvec((4+1):end))
whicj implies that newnumvec(5:end), how to get 5 index elemnet, newnumvec having 4 only.
Simmilar, when n=1 (first iterartion)
newnumvec(1:(n-1))
which becomes newnumvec(1:(1-1))>> newnumvec(1:0), zero indexing concept is not allowed in Matlab.
newnumvec(1:0) statment is invalid
I have removed those n+1 and n-1 from the if statement, now it runs without coding error
number=4391
currentNum=number
numvec=zeros(4,1)
for b=1:4
currentRem=mod(currentNum,10);
numvec(b)=currentRem
currentNum=(currentNum-currentRem)/10
end
empty=zeros(4,1);
newnumvec=numvec
maximum=zeros(3,1);
for n=1:4
for s=1:4
if newnumvec(1)>newnumvec(2:4) | newnumvec(n)>newnumvec(n:end) & newnumvec(n)>newnumvec(1:n) | newnumvec(4)>newnumvec(1:3) %here the error occurs
maximum(s)=newnumvec(n)
empty(n)=newnumvec(n)
newnumvec=newnumvec-empty
end %if
end %for
end %for
Hope it helps!
Arsen Korpetayev
am 17 Dez. 2019
1 Kommentar
KALYAN ACHARJYA
am 17 Dez. 2019
I don't know if you are getting paid for this.
We are providing volunteering service here, if my anwer help to resolved the issue, you can give due credit by accepting the answer.
Cheers !
Diese Frage ist geschlossen.
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!