Incorrect use of '=' operator for an IF statement
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
I am trying to run a for loop which will collect values 5 values before and 5 value after a certain level.
At the minute when the code looks for the next 5 values it exceeds the array, therefore I have tried to create an if statement that will only search for the 5 values before is the index exceeds the array.
At the minute the if and elseif statement recieves this error message
File: Increasing_Code.m Line: 79 Column: 30
Incorrect use of '=' operator. Assign a value to a variable using '=' and compare values for equality using '=='.
This is what I have so far.
Level_Either_Side = [];
for p = 1:length(Componentry_New);
if Position(1,p)+5 > length(Componentry_New)
Level_Either_Side = [Level_Either_Side Sorted_Level(Position(1,p)-5:Position(1,p),1)];
elseif Level_Either_Side = [Level_Either_Side Sorted_Level(Position(1,p)-5:Position(1,p)+5,1)];
end
end
If anybody nows where the mistake I have made is that would be greatly apprieciated.
0 comentarios
Respuestas (1)
Star Strider
el 20 de Mayo de 2022
I usually do something like this in setting an index range —
L = numel(Componentry_New)
for k = 1:Whatever
Level_Either_Side = max(1,Level_Either_Side Sorted_Level(Position(1,p)-5) : min(Position(1,p)+5,L);
ThisIteration{k} = SomeFunction(Componentry_New(Level_Either_Side));
end
Illustrating this approach —
v = randi(9,1,10)
L = numel(v);
for k = 1:9
idxrng = max(1,k-3) : min(k+3,L)
q = v(idxrng)
fprintf('\n----------\n')
end
Using the min and max functions this way guarantees thet the indices referenced in ‘Level_Either_Side’ never exceeds the index range of the vector being referenced.
.
0 comentarios
Ver también
Categorías
Más información sobre Matrix Indexing 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!