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!