How to Remove Specific Rows in a Multi-Column Array Based on One Column's Values
2 views (last 30 days)
Show older comments
Hello,
I am trying to remove specific rows in an array based on the values in the second column, but when it removes those rows, it also removes the first column.
What I have:
data =
1 0.002
2 0.304
3 0.220
. .
. .
1207 0.120
1208 0.043
% the first column of data is 1:1:1208;
And what I want is for all rows where the second column value is <0.1 (for example) to be removed resulting in:
data =
2 0.304
3 0.220
1207 0.120
But what I am getting is:
data =
0.304
0.220
0.120
so, I'm not able to see the column 1 value that should correspond with the remaining column 2 values.
Thank you!
0 Comments
Answers (1)
Voss
on 30 Aug 2024
data = [1:10; 0.3*rand(1,10)].'
idx = data(:,2) < 0.1;
data(idx,:) = []
The removal of the rows could also effectively be done by
data = data(~idx,:)
I don't have your code, so I can't know what you did differently, but it seems like you did
data = data(~idx,2)
2 Comments
Voss
on 30 Aug 2024
You're welcome! Any questions, let me know. Otherwise, please "Accept" this answer. Thanks!
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!