Reputation: 179
counter=1;
for i=1:50,
if y(i)<U && y(i)>L
Y(counter)=[y(i)]; % To copy the data from materix y to Y
counter=counter+1;
end
end
My question is:
Is there any way to reduce the lines of the code and use something instead of "counter" doing the same idea?
Note: U
and L
are numbers.
Upvotes: 1
Views: 51
Reputation: 2256
Use logical indexing instead.
U=50;
L = 1;
Create some random values. I multiply it with 100 to get a larger range.
A=rand(1,10).*100
A =
Columns 1 through 9:
92.3313 32.6929 33.4143 21.4837 71.6719 30.4625 7.5700 57.0943 6.4849
Column 10:
28.0583
Apply logical indexing
B=A(A<U & A>L)
B =
32.6929 33.4143 21.4837 30.4625 7.5700 6.4849 28.0583
Then you can use find and ismember to find the index if you want.
find(ismember(A,B)==1)
ans =
2 3 4 6 7 9 10
Upvotes: 3