Info

This question is closed. Reopen it to edit or answer.

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 view (last 30 days)
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

Answers (2)

KALYAN ACHARJYA
KALYAN ACHARJYA on 16 Dec 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 Comments
Arsen Korpetayev
Arsen Korpetayev on 17 Dec 2019
Thank you for your answer, Sir. However, when using inequality signs in an if statement between a scalar and a vector, Matlab percieves it as a command to compare each element of the vector with that scalar. This is exactly what I am trying to achieve. I have checked this theory with a simple code like this:
a=[1.5; 3]
b=1
if b<a(1:2)
fprintf works
end
Therefore I am puzzled as where the problem with "matrix dimensions" lies and how it can approached. Many thanks for your answer nevertheless. Merry Christmas and a happy licking off leftover yogurt from a plastic package
KALYAN ACHARJYA
KALYAN ACHARJYA on 17 Dec 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
Arsen Korpetayev on 17 Dec 2019
Dear Kalyan, my sincere graditude for your response once again, although I don't know if you are getting paid for this. I see where the error was coming from, thank you for clarifying this. However, it's worth to note that a statement a(n)>a(n:end) will be false for any values, since a number cannot be greater than itself. I am closing this question, since I had relised that the above approach is not only inefficient, but seemingly incapable in its nature, because of the very discussed restrictions. Have a great winter season and don't forget to clean your coffee table. All the best, your sincere Arsen Korpetayev.
  1 Comment
KALYAN ACHARJYA
KALYAN ACHARJYA on 17 Dec 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 !

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!