Condition for matlab if

조회 수: 4 (최근 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