Find numeric columns in a cell array

19 visualizaciones (últimos 30 días)
Sergio Rossi
Sergio Rossi el 3 de Jul. de 2014
Comentada: Bilirubin el 19 de Sept. de 2018
Hi,
I would like to use gplotmatrix on a dataset data, which contains mixed data (numeric and strings). However, gplotmatrix works on numeric data, so I need to convert my dataset to a matrix. As far as I understand, the only way is to do this is by
C=dataset2cell(data)
X=cell2mat(C)
However, the second command doesn't work, because C contains non-numeric columns. Is there a way to find which columns of a cell array contains only numbers? Thanks a lot,
Sergio

Respuesta aceptada

Jos (10584)
Jos (10584) el 3 de Jul. de 2014
% C is a cell array, like
C = {1 2 3 4 5 ; 11 '12' 13:15 14 []}
q = cellfun(@(x) isnumeric(x) && numel(x)==1, C) % true for elements of C that are numerical scalars
IsAllNum = all(q,1) % true for columns of C that have only numerical scalars
X = cell2mat(C(:,IsAllNum)) % select and convert
  1 comentario
Bilirubin
Bilirubin el 19 de Sept. de 2018
Does anyone know why isnan gives an error msg?
C = {1 2 3 4 5 ; 11 '12' 13:15 14 []}
q = cellfun(@(x) isnan(x) && numel(x)==1, C) % true for elements of C that are numerical scalars

Iniciar sesión para comentar.

Más respuestas (1)

Pascal
Pascal el 3 de Jul. de 2014
If you know whole columns are either numeric or not,
cell2mat(C(:,find(cellfun(@isnumeric, C(1,:)))))

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by