MATLAB Answers

Delete certain rows of a matrix based on specific values

145 views (last 30 days)
Aude Rapet
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
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!

  0 Comments

Sign in to comment.

Accepted Answer

Niels
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

  4 Comments

Show 1 older comment
Aude Rapet
Aude Rapet on 1 Feb 2017
@Niels What I try to do is to select only the positions x y detected in the corners of my picture dimension 680*512 pixels. But if I do this, I delete everything:
res_final(res_final(:,1)>100 & res_final(:,2)>100,:) =[]; %corner left top
res_final(res_final(:,1)>100 & res_final(:,2)<420,:)=[]; %corner left bottom
res_final(res_final(:,1)<550 & res_final(:,2)>100,:)=[]; %corner right top
res_final(res_final(:,1)<550 & res_final(:,2)<420,:)=[]; %corner right bottom
Do you know how I could this, maybe with a "or" instead of "&" ?
Niels
Niels on 1 Feb 2017
example=randi(100,6,6)
% get a grid/matrix size(Ysize,Xsize) in each corner
% example=imread('replace.png')
[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
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.

Sign in to comment.

More Answers (0)

Sign in to answer this question.