how to delet multiple rows with single criteria in matlab

1 view (last 30 days)
I have 900 by 4 mixed data. The first column contains letter of atom and the rest column is the XYZ coordinates of the atom. The arrangement of the atom looks like H1;H2;H3.......... this three rows repeat it self until the number of rows are 900.
x y Z
H1 1 2 3
*H2 2 4 7*
H3 3 0 0
H1 1 1 1
*H2 0 0 0*
H3 4 6 7
.
.
.
.
So how can i remove those three rows ( H1;H2;H3) if x of H2 >=3 else keep it.the data is a cell array.The main point is i only take as a criteria the value of x corresponds to H2. From 900 rows 300 of them are H2.For instance If the first H2(second row) satisfy the criteria i want to delet H1;H2;H3 and got the next H2.If it doesn't satisfy the criteria i will keep the three rows and go to the next H2.

Accepted Answer

Guillaume
Guillaume on 18 Mar 2015
Edited: Guillaume on 18 Mar 2015
Step 1: reshape the cell array so each row is H1 x y z H2 x y z H3 x y z:
new_c = reshape(c', 12, [])';
Step 2: find out which of the row of the reshaped cell array has x of H2 (column 6) above your threshold
rowstodelete = cell2mat(new_c(:, 6)) > 3;
Step 3: delete these rows:
new_c(rowstodelete, :) = [];
Step 4: reshape back to original n*4 columns
new_c = reshape(new_c', 4, [])'
  1 Comment
alex solomon
alex solomon on 18 Mar 2015
Thank you so much!! If it is not to much to ask , how can i count the number of H1,H2 and H3 and the Total Number=H1+H2+H3 and put as a row header in my data. Firs row total number, 2nd row H1, 3rd row H2 and 4th row H3.

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 18 Mar 2015
Edited: Image Analyst on 18 Mar 2015
Assuming your data is in cell array "data"
x = data{:, 2}; % Extract column 2.
rowsToDelete = x >= 3;
filteredData = data(~rowsToDelete, :);

James Tursa
James Tursa on 18 Mar 2015
Edited: James Tursa on 18 Mar 2015
Are you trying to delete all three H1, H2, H3 rows if any of the x values in those rows are >= 3? If so, in your example it looks like all the rows will be deleted since the first set has H3 x = 3 and the 2nd set has H3 x = 4. Is this correct? And is your data in a cell array? If so, here is one way:
c = your cell array;
xlimit = 3; % or whatever
x = repmat(any(reshape(cell2mat(c(:,2)),3,[])>=xlimit),3,1);
c(x(:),:) = [];
  2 Comments
alex solomon
alex solomon on 18 Mar 2015
Sorry there was a error in my first question. Here is the correct one. So how can i remove those three rows ( H1;H2;H3) if x of H2 >=3 else keep it.the data is a cell array.The main point is i only take as a criteria the value of x corresponds to H2. From 900 rows 300 of them are H2.For instance If the first H2(second row) satisfy the criteria i want to delet H1;H2;H3 and got the next H2.If it doesn't satisfy the criteria i will keep the three rows and go to the next H2.
James Tursa
James Tursa on 18 Mar 2015
So something like this?
c = your cell array;
xlimit = 3; % or whatever
x = repmat([c{2:3:end,2}]>=xlimit,3,1);
c(x(:),:) = [];

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!