Condition for matlab if

4 visualizaciones (últimos 30 días)
Lev Mihailov
Lev Mihailov el 24 de Jun. de 2019
Comentada: infinity el 24 de Jun. de 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?

Respuestas (3)

infinity
infinity el 24 de Jun. de 2019
Hello
How about if you remove semi-colon in the line
if g(i)+20>size(Wscat,1);
  2 comentarios
Lev Mihailov
Lev Mihailov el 24 de Jun. de 2019
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 el 24 de Jun. de 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".

Iniciar sesión para comentar.


Steven Lord
Steven Lord el 24 de Jun. de 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
Star Strider el 24 de Jun. de 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.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by