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

조회 수: 4 (최근 30일)
AJ
AJ 2024년 8월 30일
댓글: Voss 2024년 8월 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)
  댓글 수: 2
AJ
AJ 2024년 8월 30일
Thank you so much, this is perfect. I was doing
data = data(~idx,2)
Voss
Voss 2024년 8월 30일
You're welcome! Any questions, let me know. Otherwise, please "Accept" this answer. Thanks!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by