Info
此问题已关闭。 请重新打开它进行编辑或回答。
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 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
KALYAN ACHARJYA
2019-12-16
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 个评论
KALYAN ACHARJYA
2019-12-17
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
2019-12-17
1 个评论
KALYAN ACHARJYA
2019-12-17
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 !
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!