deleting matrix rows by criteria?

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
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) ?
Peter
Peter el 22 de Feb. de 2014
the cutoff values are determined by outside experimental data, such that it is a simple set of ranges

Iniciar sesión para comentar.

Respuestas (1)

Azzi Abdelmalek
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

Peter
Peter el 22 de Feb. de 2014
Okay, that works for the first row of the matrix. how would I go about applying that to the whole matrix, considering that the values for the range change?
Azzi Abdelmalek
Azzi Abdelmalek el 22 de Feb. de 2014
Editada: Azzi Abdelmalek el 22 de Feb. de 2014
This works for all rows that matches your criterion. Have you tested the code?
Jan
Jan el 22 de Feb. de 2014
Editada: Jan el 22 de Feb. de 2014
@Azzi: The problem might be, that Peter dis not specify, what this exactly means:
now the tricky part(for me anyway) is that the criteria is different for every row.
@Peter: Please elaborate this by editing the original question.
Peter
Peter el 22 de Feb. de 2014
edited, have a look
n=3 % number of criterion
for k=1:size(M,1)
kk=mod(k,n);
switch kk
case 1
idx=55<M(:,1)& M(:,1)<65;
case 2
idx=0<M(:,2) & M(:,2)<4;
case 0
idx=50<M(:,1)& M(:,1)<60;
end
M(idx,:)=[];
end
Peter
Peter el 22 de Feb. de 2014
if its not asking too much, can you walk me through this code? I dont quite get it
Peter
Peter el 22 de Feb. de 2014
also, I find that if i use your initial suggestion:
out=M(55<M(:,1)& M(:,1)<65 & 0<M(:,2) & M(:,2)<4,:)
I could do it 8 times and get 8 matrices that have my correct rows. is there a way to interlace them this way:
matrix =
Row 1(from matrix 1)
Row 1(from matrix 2)
Row 1(from matrix 3)
Row 1(from matrix 4)
Row 1(from matrix 5)
Row 1(from matrix 6)
Row 1(from matrix 7)
Row 1(from matrix 8)
Row 2(from matrix 1)
Row 2(from matrix 2)
Row 2(from matrix 3)
Row 2(from matrix 4)
and so on
this is just another way of looking at it, tho if you can explain to me how your loop works i will be very grateful
Azzi Abdelmalek
Azzi Abdelmalek el 23 de Feb. de 2014
Look at edited answer

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 22 de Feb. de 2014

Comentada:

el 23 de Feb. de 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by