If statement is not triggering correctly

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 Commento

Do we know for sure that i and j are scalars?
Using length(M(:,1)) is not recommended. Use size(M,1) instead.

Accedi per commentare.

Categorie

Scopri di più su MATLAB in Centro assistenza e File Exchange

Richiesto:

il 26 Nov 2019

Modificato:

il 26 Nov 2019

Community Treasure Hunt

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

Start Hunting!

Translated by