Selecting rows from a table using a column that includes Nan elements
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaire
Réponse acceptée
Guillaume
le 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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Numeric Types dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!