How to Remove Specific Rows in a Multi-Column Array Based on One Column's Values

3 次查看(过去 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!

回答(1 个)

Voss
Voss 2024-8-30
data = [1:10; 0.3*rand(1,10)].'
data = 10x2
1.0000 0.0852 2.0000 0.0717 3.0000 0.2240 4.0000 0.1382 5.0000 0.0334 6.0000 0.2094 7.0000 0.2247 8.0000 0.2036 9.0000 0.1893 10.0000 0.0435
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
idx = data(:,2) < 0.1;
data(idx,:) = []
data = 6x2
3.0000 0.2240 4.0000 0.1382 6.0000 0.2094 7.0000 0.2247 8.0000 0.2036 9.0000 0.1893
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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)

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by