# Condition for matlab if

2 views (last 30 days)
Lev Mihailov on 24 Jun 2019
Commented: infinity on 24 Jun 2019
[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?

infinity on 24 Jun 2019
Hello
How about if you remove semi-colon in the line
if g(i)+20>size(Wscat,1);
##### 2 CommentsShowHide 1 older comment
infinity on 24 Jun 2019
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 on 24 Jun 2019
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 on 24 Jun 2019
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.

### Categories

Find more on Logical in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!