필터 지우기
필터 지우기

Filter value from multiple columns

조회 수: 16 (최근 30일)
Augusto Gabriel da Costa Pereira
댓글: Mathieu NOE 2023년 2월 9일
I have the following array named "tNino1_2"
I want to filter the value equal to 1 from columns 20, 21, 22, 23 and 24
The script I'm using is below:
xcol=20:1:24
idx=tNino1_2(tNino1_2(:,xcol)==1,:)
The logical indices in position 1 contain a true value outside of the array bounds.
  댓글 수: 4
Dyuman Joshi
Dyuman Joshi 2023년 2월 9일
Do you want to find the position (both row and column values) of elements equal to one between columns 20 to 24?
Augusto Gabriel da Costa Pereira

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

채택된 답변

Mathieu NOE
Mathieu NOE 2023년 2월 9일
hello
I assumed you want all the rows where there is at least one 1 in the columns 20 to 24
xcol=20:1:24;
[r,c] = find(tNino1_2(:,xcol)==1);
[ru,ia,ic] = unique(r);
out = tNino1_2(ru,:);
  댓글 수: 2
Augusto Gabriel da Costa Pereira
This script is exactly what I wanted, it worked perfectly.
Thank you Mathieu, you are accurate as always.
Mathieu NOE
Mathieu NOE 2023년 2월 9일
hello Augusto
as always, my pleasure !

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

추가 답변 (1개)

Voss
Voss 2023년 2월 9일
편집: Voss 2023년 2월 9일
load tNino1_2
"find the values equal to 1 in columns 20 to 24"
[r,c] = find(tNino1_2(:,20:24) == 1)
r = 28×1
96 97 98 107 164 174 107 164 174 175
c = 28×1
1 1 1 1 1 1 2 2 2 2
r is rows and c is columns. Of course, c is relative to columns 20-24 only, so c = 1 means column 20 of the original matrix, c = 2 means column 21, etc.
You can add 19 to c to get the locations of the 1s in the original matrix
c = c+19
c = 28×1
20 20 20 20 20 20 21 21 21 21
Confirm that the elements at those locations are all 1s:
temp = tNino1_2(sub2ind(size(tNino1_2),r,c))
temp = 28×1
1 1 1 1 1 1 1 1 1 1
all(temp == 1)
ans = logical
1
  댓글 수: 1
Augusto Gabriel da Costa Pereira
thank you, this script is perfect to identify the rows and columns of the condition I entered

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by