How do I convert a cell array with string, double, and NaN values to a double array

5 vues (au cours des 30 derniers jours)
I have a cell with these values = {1, 2, 'None', 1, 2, 3, 5, NaN, 'None'}
I want to convert this into a double with these values = {1, 2, NaN, 1, 2, 3, 5, NaN, NaN}
Is this possible?

Réponse acceptée

jonas
jonas le 5 Sep 2018
Modifié(e) : jonas le 5 Sep 2018
My original solution was obviously flawed, so credits to Guillaume for cleaning up redundant functions
A(strcmp(A, 'None')) = {NaN};
cell2mat(A)
... and credits to Stephen Cobeldick for providing the general solution (for any string):
A(~cellfun(@isnumeric,A)) = {NaN}
  3 commentaires
Stephen23
Stephen23 le 5 Sep 2018
Modifié(e) : Stephen23 le 5 Sep 2018
No need to use find, logical indexing works perfectly.
An alternative that replaces all non-numeric values:
A(~cellfun(@isnumeric,A)) = {NaN}
jonas
jonas le 5 Sep 2018
Thanks guys! I put your solutions in the answer box for future reference, as the answer had already been accepted.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by