# Problem with if statement

6 visualizaciones (últimos 30 días)
Adam Dutkiewicz el 16 de Mayo de 2021
Comentada: Adam Dutkiewicz el 16 de Mayo de 2021
minval=0
maxval=1
r=[0,1,2,3,4]
for i:1:length(r)
if r(i)>=minval & r(i) maxval>=r(i)
disp('Value within specified range.')
elseif (r > maxval)
disp('Value exceeds maximum value.')
r(i)=()
else
disp('Value is below minimum value.')
r(i)=()
end
end
r
So this is a body of a program that i want to use in some bigger one, but my problem is that everytime i want to compute it i get error:
" invalid left hand side of assignment
>>> if r(i)>=minval & r(i) maxval>=r(i)
^"
I have no clue what's wrong with this code, I'm pretty sure i used exactly the same code in the past and it was working, anyone has idea what I'm doing wrong?
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Respuesta aceptada

David Fletcher el 16 de Mayo de 2021
Editada: David Fletcher el 16 de Mayo de 2021
There are numerous errors in the code, the condition on the if statement makes no sense I assume you mean:
if r(i)>=minval & maxval<=r(i)
The for loop syntax is invalid, I assume you mean
for i=1:1:length(r)
These lines in the alternative conditionals are invalid syntax (and even if they were valid I have no idea what their purpose is) - maybe a filter to omit out of range values from the input vector? If this is the case it wouldn't work - you would be better off building a new vector containing valid values, rather than delete values from the existing vector since this will make the vector smaller and lead to an indexing error as the loop progresses through the vector.
r(i)=(); %maybe you mean r(i)=[], but this wouldn't work in the context of this program
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Adam Dutkiewicz el 16 de Mayo de 2021
Thank you very much it worked! I also used your suggestion to create a new variable to store the values, you are right that's better to do in this way.
Thank you very much again

Iniciar sesión para comentar.

### Categorías

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

### Community Treasure Hunt

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

Start Hunting!

Translated by