Incorrect use of '=' operator for an IF statement
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (1)
Star Strider
le 20 Mai 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 commentaires
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!