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 个评论

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

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 MATLAB 的更多信息

标签

提问:

2019-11-26

编辑:

2019-11-26

Community Treasure Hunt

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

Start Hunting!

Translated by