how to delet multiple rows with single criteria in matlab
1 view (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
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, [])'
More Answers (2)
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, :);
0 Comments
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
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(:),:) = [];
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!