how extract table RowNames based on table elements

조회 수: 21 (최근 30일)
Yunwei
Yunwei 2023년 2월 22일
답변: Steven Lord 2023년 2월 22일
Hello all,
I have a table of only one colume with RowNames.
The table have numerical values and NaN.
I want to extract the RowNames of all of the NaN in the table. How can I do that?
Thanks in advance!

채택된 답변

Voss
Voss 2023년 2월 22일
% a table as you describe
t = table([1;NaN;3;4;NaN],'RowNames',{'A';'B';'C';'D';'E'})
t = 5×1 table
Var1 ____ A 1 B NaN C 3 D 4 E NaN
% extract RowNames of the NaN rows
nan_rows = t.Properties.RowNames(isnan(t{:,:}))
nan_rows = 2×1 cell array
{'B'} {'E'}

추가 답변 (1개)

Steven Lord
Steven Lord 2023년 2월 22일
I'd use ismissing, to avoid the need to extract the whole contents of the table into a separate array.
% a table as you describe
t = table([1;NaN;3;4;NaN],'RowNames',{'A';'B';'C';'D';'E'})
t = 5×1 table
Var1 ____ A 1 B NaN C 3 D 4 E NaN
% extract RowNames of the NaN rows
nan_rows = t.Properties.RowNames(ismissing(t))
nan_rows = 2×1 cell array
{'B'} {'E'}
This also works if t has variables of different types. Yes, I could have listed "elderberry" but I need a missing value for the example.
t.FruitNames = ["apple"; "banana"; "cherry"; "date"; missing]
t = 5×2 table
Var1 FruitNames ____ __________ A 1 "apple" B NaN "banana" C 3 "cherry" D 4 "date" E NaN <missing>
ismissing(t)
ans = 5×2 logical array
0 0 1 0 0 0 0 0 1 1
rowWithMissingNumberAndFruit = t.Properties.RowNames(all(ismissing(t), 2))
rowWithMissingNumberAndFruit = 1×1 cell array
{'E'}
rowWithMissingNumberOrFruit = t.Properties.RowNames(any(ismissing(t), 2))
rowWithMissingNumberOrFruit = 2×1 cell array
{'B'} {'E'}
The approach Voss posted wouldn't work here.
isnan(t{:,:})
Incorrect number or types of inputs or outputs for function 'isnan'.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by