HELP : Subscript indices must either be real positive integers or logicals - AGAIN
1 次查看(过去 30 天)
显示 更早的评论
I need your help to solve the problem of subscript index that must either be positive integer or logical. Can any one help to correct this ?
A = magic(5);
M1 = size(A,1);
M2 = size(A,2);
E = [5 8 20];
for x = 1:M1
for y = 1:M2
if A(A(x,y) == E)
A(x-1,y) = 5
end
end
end
Thank you!
0 个评论
采纳的回答
Adam
2016-1-11
By including 'again' in the title I hope that you are learning from previous solutions and that this is a different type of cause to your previous such problems.
The line
A(x-1,y) = 5
fails first time round the loop because x is 1 so x-1 is 0 which is not a valid array index (this is what the error message tells you - it must be a real positive integer or a logical - 0 is neither of those).
How to fix it depends on your intent. Starting your for loop indexed from 2 instead of 1 would remove the error but may not be the solution you want.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!