Condition for matlab if

2 次查看(过去 30 天)
Lev Mihailov
Lev Mihailov 2019-6-24
评论: infinity 2019-6-24
[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?

回答(3 个)

infinity
infinity 2019-6-24
Hello
How about if you remove semi-colon in the line
if g(i)+20>size(Wscat,1);
  2 个评论
Lev Mihailov
Lev Mihailov 2019-6-24
Deleted, I think it is worth making a cycle if less than 20 then = g + 21, if more then f
[d,f] = max(Wscat,[],1);
for i = 1:length(d)
if f(i)>20
g(i)=f(i)
else f(i)<20
g(i)=f(i)+21
end
end
But I do not understand why my cycle does not work?
infinity
infinity 2019-6-24
I think should refer answers below since g(i) - 50 may less than or equal to 0. Then, you should think how to modify the condition statement in "if - elseif - else".

请先登录,再进行评论。


Steven Lord
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?

Star Strider
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.

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by