Condition for matlab if
2 次查看(过去 30 天)
显示 更早的评论
[d,g] = max(Wscat,[],1);
for i = 1:length(d)
if g(i)+20>size(Wscat,1);
PP=Wscat((g(i)-50:g(i)),i) ;
P{i}=PP(PP ~= 0);
elseif g(i)-50<0 % condition when g = 1 minus 50
PP=Wscat(1:g(i),i) ; % In my matrix, some values are g = 1 and 1-50
P{i}=PP(PP ~= 0);
else
PP=Wscat((g(i)-50:g(i)),i) ; % Error Subscript indices must either be real positive integers or logicals.
P{i}=PP(PP ~= 0);
end
end
Error Subscript indices must either be real positive integers or logicals.
How to fix this problem?
0 个评论
回答(3 个)
Steven Lord
2019-6-24
PP=Wscat((g(i)-50:g(i)),i) ;
If g(i) is exactly equal to 50, this will ask for the 0th element of Wscat. Arrays in MATLAB don't have 0th elements; the first element is 0. 0 is not a "real positive integer".
Does your data ever have g(i) exactly equal to 50? Does it reach this section of your if/elseif/else construct if it does?
0 个评论
Star Strider
2019-6-24
I suspect that here:
PP=Wscat((g(i)-50:g(i)),i) ; % Error Subscript indices must either be real positive integers or logicals.
the result of:
g(i)-50
is going to be negative or 0, neither of which are allowed in MATLAB subscripts.
Without knowing what those values are, it is not possible to write specific code to correct the problem.
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!