필터 지우기
필터 지우기

Remove rows from a string array that correspond to rows being thresholded in a separate numeric matrix

조회 수: 13 (최근 30일)
I have a string array and a separate numeric matrix. The string array is one column with many rows and the numeric matrix is two columns with many rows (the same number of rows as the string array). I want to threshold some of the rows of the numeric matrix, but if a row is removed from the numeric matrix I want the corresponding row number to be removed from the string array. For example, remove any rows where the value is < 0.05 in both columns of the numeric matrix. If rows 2,3,4 meet this condition in the numeric array then remove those rows from the string array as well. Thank you.

채택된 답변

Cris LaPierre
Cris LaPierre 2021년 7월 7일
Use an index to capture the rows that meet your criteria, and then use that index to delete the corresponding rows from both arrays.
txt = ["A";"B";"C"];
num = [1 2; 3 4; 5 6];
% find rows that meet criteria
ind = num(:,2)==4;
% remove rows from string array and matrix
txt(ind) = []
txt = 2×1 string array
"A" "C"
num(ind,:) = []
num = 2×2
1 2 5 6
  댓글 수: 2
Jordan
Jordan 2021년 7월 7일
What if I need the criteria to be met across multiple columns? How would I index the rows that meet that criteria? For example, index rows whose elements are equal to 4 in column 1 and 2?
Cris LaPierre
Cris LaPierre 2021년 7월 7일
Just add additional criteria to your expression. In this case, none of the rows meet both criteria, so nothing is removed.
txt = ["A";"B";"C"];
num = [1 2; 3 4; 5 6];
% find rows that meet criteria
ind = num(:,2)==4 & num(:,1)==4;
% remove rows from string array and matrix
txt(ind) = []
txt = 3×1 string array
"A" "B" "C"
num(ind,:) = []
num = 3×2
1 2 3 4 5 6

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by