Delete certain rows of a matrix based on specific values

5 views (last 30 days)
Aude Rapet on 31 Jan 2017
Commented: Aude Rapet on 2 Feb 2017
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
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,:) = [] ;

Niels on 31 Jan 2017
Edited: Niels on 31 Jan 2017
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
Niels on 1 Feb 2017
example=randi(100,6,6)
% get a grid/matrix size(Ysize,Xsize) in each corner
[i,j]=size(example);
Xsize=2; % Gridrange #colums
Ysize=3 % # rows
UpRight=example(1:Ysize,j+1-Xsize:j)
UpLeft=example(1:Ysize,1:Xsize)
DownRight=example(i+1-Ysize:i,j+1-Xsize:j)
DownLeft=example(i+1-Ysize:i,1:Xsize)
maybe like this?
Aude Rapet on 2 Feb 2017
Thanks. What you told me before works really good to have the four corners :
res_final = res_select ;
res_final(res_final(:,1)>100 & res_final(:,1)<550,:)=[]; % deletes x between 100 and 550
res_final(res_final(:,2)>100 & res_final(:,2)<420,:)=[]; % delete y between 100 and 420
But I was looking for a way to be able to "select" the corner I wanted to keep. For example have bottom right and top left only. With this code I have four of them. I am not sure it is possible though.