How to remove all rows from categorical variable corresponding to a double array?

3 ビュー (過去 30 日間)
Anthony
Anthony 2018 年 3 月 9 日
回答済み: Peter Perkins 2018 年 3 月 15 日
Assuming the following data and Table X:
A=[ 1 5 0 3 9]';
B=[ 2 9 5 8 0 ]';
C={'S1','S2','S3','S4','S5'}';
C = categorical(C);
X = table(C,A,B);
...and removing the rows with zeros
A1 = A(A(:,1) > 0, :);
B1 = B(B(:,1) > 0, :);
How do I now remove the corresponding rows from C that were removed in A and B, and make a new table X1 that contains A1, B1, and C1? I know have have to using indexing to determine which categories of C correspond to the rows that were removed, I'm just not sure how to do that.

採用された回答

Fangjun Jiang
Fangjun Jiang 2018 年 3 月 9 日
index=and(A(:,1) > 0,B(:,1) > 0)
X1=X(index,:)

その他の回答 (1 件)

Peter Perkins
Peter Perkins 2018 年 3 月 15 日
You might find this more expressive, and it doesn't require the original vectors, which you may have chosen to delete afer putting them in the table X:
X2 = X(X.A>0 & X.B>0,:)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by