How to Remove Specific Rows in a Multi-Column Array Based on One Column's Values
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Voss
2024-8-30
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 个评论
Voss
2024-8-30
You're welcome! Any questions, let me know. Otherwise, please "Accept" this answer. Thanks!
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!