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 个评论
Walter Roberson
2019-11-26
Do we know for sure that i and j are scalars?
Using length(M(:,1)) is not recommended. Use size(M,1) instead.
回答(1 个)
Walter Roberson
2019-11-26
all([i,j] >= 1) && all([i,j] <= size(M,1))
类别
在 帮助中心 和 File Exchange 中查找有关 MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!