Delete certain rows of a matrix based on specific values
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Niels
le 31 Jan 2017
Modifié(e) : Niels
le 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 commentaires
Niels
le 1 Fév 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?
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!