MATLAB: Delete certain rows of a matrix based on specific values

delete rowlogical indexingloopmatrixmatrix manipulation

Hi,
I am a beginner in Matlab. I have a matrix that has 2 columns and thousands of rows.
I need to delete the rows based on the following conditions:
1. if a value of column 1 is superior at 100 and inferior at 550 => delete this row
2. if a value of column 2 is superior at 100 and inferior at 420 => delete this row
Based on
http://au.mathworks.com/matlabcentral/answers/105768-how-can-i-delete-certain-rows-of-a-matrix-based-on-specific-column-values#answer_226615
I tried :
% Specify conditions

TF1 = (100 < res_select(:,1)) & (res_select(:,1) < 550) ;
TF2 = (100 < res_select(:,2)) & (res_select(:,2) < 420) ;
% combine them

TFall = TF1 & TF2 ;
% remove

res_final = res_select ;
res_final(TFall,:) = [] ;
But it's not working I don't understand why. Should I try to create a loop instead of logical indexing ? I tried this but not working neither :
% Specify conditions
for i = 1:length(res_select(:,1))
TF1 = (100 < res_select(i,1)) & (res_select(i,1) < 550) ;
end
for i = 1:length(res_select(:,2))
TF2 = (100 < res_select(i,2)) & (res_select(i,2) < 420) ;
end
% combine them
TFall = TF1 & TF2 ;
% remove
res_final = res_select ;
res_final(TFall,:) = [] ;
Thank you for your help!

Best Answer

Hi, i would call this a rather bitter solution for a beginner to understand, but it is short and it works, if you want to understand it you should first look what a<100 does when "a" is a matrix (you shouldnt use this for if statements) and then combind it with &
a=randi(900,15,2);
a(a(:,1)>100 & a(:,1)<550,:)=[]; % deletes rows dependent to first condition
a(a(:,2)>100 & a(:,2)<420,:)=[]; % ... second condition
a =
580 176
341 204
731 154
480 205
316 393
846 280
789 832
496 388
561 167
529 815
187 882
272 395
424 101
208 233
760 368
a =
580 176
731 154
846 280
789 832
561 167
760 368
a =
789 832