deleting matrix rows by criteria?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I have a matrix as follows(this is sample data from a much longer matrix)
sample_position =
58.044 1.0831
110.19 244.98
109.95 255.78
-150.57 181.66
-445.85 -160.02
-570.87 -404.5
-727.97 0.7062
-529.34 306.81
581.03 122.86
761.24 -350.04
62.561 0.98683
-150.74 185.85
-454.6 -160.39
-571.29 -402.92
-730.77 0.38753
-529.9 305.2
583.51 120.23
762.82 -350.56
I need to delete rows that do not fall under some criteria, say: 55<column1<65 & 0<column2<4
So if the row doesn't comply to both criteria, it gets deleted.
now the tricky part(for me anyway) is that the criteria is different for every row.
let me try explaining it a bit more. for the first row, the example criterion applies. for the second row, a new set of criterion exists (say 130<column1<140 & 20<column2<35)
I need to write something that will do the following:
>apply criterion 1 to row 1
>if row 1 is within criterion 1, write row 1 to output variable
>if row 1 is not within criterion 1, delete row 1 and apply criterion 1 to the next row(repeat until a row is within criterion 1 and write that row to output variable)
>apply criterion 2 to next row
PS:I have 8 different criterions. once I reach the 8th, the code needs to go back to the first critetion and repeat the process until it goes through all the rows in the matrix
any ideas?(i think there should be a loop somewhere?)
2 comentarios
Walter Roberson
el 22 de Feb. de 2014
Is the criteria of a different form for each row, or is it just with different cutoff values for each row? If so then are the cutoff values to be calculated (e.g., partly based on row number) or are the cutoff values opaque per-row constants (determined through outside information) ?
Respuestas (1)
Azzi Abdelmalek
el 22 de Feb. de 2014
Editada: Azzi Abdelmalek
el 23 de Feb. de 2014
Edit
M=[58.044 1.0831
110.19 244.98
109.95 255.78
-150.57 181.66
-445.85 -160.02
-570.87 -404.5
-727.97 0.7062
-529.34 306.81
581.03 122.86
761.24 -350.04
62.561 0.98683
-150.74 185.85
-454.6 -160.39
-571.29 -402.92
-730.77 0.38753
-529.9 305.2
583.51 120.23
762.82 -350.56]
n=3 % number of criterion
out=[];
kk=1
for k=1:size(M,1)
switch kk
case 1
idx=55<M(k,1)& M(k,1)<65;
case 2
idx=0<M(k,2) & M(k,2)<4;
case 0
idx=50<M(k,1)& M(k,1)<60;
end
if idx==1
out(end+1,:)=M(k,:);
kk=mod(kk+1,n);
end
end
disp(out)
In your case, write the variable kk will vary from 1 to 7 then 0
8 comentarios
Ver también
Categorías
Más información sobre Monte Carlo Analysis en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!