Delete certain rows of a matrix based on specific values
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Aude Rapet
el 31 de En. de 2017
Comentada: Aude Rapet
el 2 de Feb. de 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 comentarios
Respuesta aceptada
Niels
el 31 de En. de 2017
Editada: Niels
el 31 de En. de 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 comentarios
Niels
el 1 de Feb. de 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?
Más respuestas (0)
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!