Selecting rows from a table using a column that includes Nan elements
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Caglar
am 20 Nov. 2018
Beantwortet: Guillaume
am 20 Nov. 2018
Hello,
I have a table and I am using one of it's columns to select rows. All data is char. Normally I can use this,
ismember(my_table{:,6},{'SelectThese'}) %6 is column number
However, due to limitations of 'ismember', it does not work if column has NaN elements.
It says,
Error using cell/ismember (line 34)
Input A of class cell and input B of class cell must be cell arrays of character vectors, unless one is a character
vector.
Unfortunately, ismissing ignores those nans and isnan does not work on tables or cell arrays.
How can I select my rows then?
To put it differently, a column in my table has chars and nans as data:
my_table.column(1)='veri1'; my_table.column(2)='veri2'; my_table.column(3)='veri2'; my_table.column(4)=0/0; %0/0=Nan
and I want to select rows that has 'veri2' as column data.
I atached my table.
1 Kommentar
Akzeptierte Antwort
Guillaume
am 20 Nov. 2018
The real question is why have you got NaN in a column of text. It's never a good idea to mix text with numeric. The best thing would be to fix the table creation so that '' gets into that column instead of NaN. Otherwise, fix it after the fact:
%note that the shortcircuiting behaviour of && is used below to prevent calling isnan on a char array. It cannot be replaced by &.
my_table{cellfun(@(t) isnumeric(t) && isnan(t), my_table{:, 6}), 6} = {''}; %replace NaN entries by empty string.
You can then use ismember as usual.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Cell Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!