If statement is not triggering correctly
Show older comments
function Mnew = exchangeRows(M,i,j)
if i <=0 && j <=0 || i > length( M(:,1)) && j > length( M(:,1)) || j <=0 && i > length( M(:,1)) || i <=0 && j > length( M(:,1))
warning('Row i and j are not valid')
elseif i <=0 || i > length ( M(:,1) )
warning('Row i is not valid')
elseif j <=0 || j > length ( M(:,1) )
warning('Row j is not valid')
For some reason is the first if statement is triggered when My i>=0 ,BUT my j is not >=0 or larger than the length of the matrix.
the same happens when my j is greater than the length of the matrix, BUT my i is not >=0 or larger than the matrix.
First i wrote the statement like this:
if i <=0 || i > length ( M(:,1) ) && j <=0 || j > length ( M(:,1) )
but this brought the same problem thats why i try the rewrite it, without luck unfortunatley :C.
it is really strange because for all the other inputs it works fine
Update ive changed all the lengths with size(M,1), but this does not resolve the problem
1 Comment
Walter Roberson
on 26 Nov 2019
Do we know for sure that i and j are scalars?
Using length(M(:,1)) is not recommended. Use size(M,1) instead.
Answers (1)
Walter Roberson
on 26 Nov 2019
all([i,j] >= 1) && all([i,j] <= size(M,1))
Categories
Find more on MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!