Incorrect use of '=' operator for an IF statement

1 view (last 30 days)
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.

Answers (1)

Star Strider
Star Strider on 20 May 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)
v = 1×10
8 4 8 5 8 7 6 7 5 6
L = numel(v);
for k = 1:9
idxrng = max(1,k-3) : min(k+3,L)
q = v(idxrng)
fprintf('\n----------\n')
end
idxrng = 1×4
1 2 3 4
q = 1×4
8 4 8 5
----------
idxrng = 1×5
1 2 3 4 5
q = 1×5
8 4 8 5 8
----------
idxrng = 1×6
1 2 3 4 5 6
q = 1×6
8 4 8 5 8 7
----------
idxrng = 1×7
1 2 3 4 5 6 7
q = 1×7
8 4 8 5 8 7 6
----------
idxrng = 1×7
2 3 4 5 6 7 8
q = 1×7
4 8 5 8 7 6 7
----------
idxrng = 1×7
3 4 5 6 7 8 9
q = 1×7
8 5 8 7 6 7 5
----------
idxrng = 1×7
4 5 6 7 8 9 10
q = 1×7
5 8 7 6 7 5 6
----------
idxrng = 1×6
5 6 7 8 9 10
q = 1×6
8 7 6 7 5 6
----------
idxrng = 1×5
6 7 8 9 10
q = 1×5
7 6 7 5 6
----------
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.
.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!