Converting Cell matrix to a Numeric Matrix

3 vues (au cours des 30 derniers jours)
Souarv De
Souarv De le 12 Mai 2021
Commenté : Souarv De le 12 Mai 2021
I have a cell matrix as shoown below.
B={'2' '3' '5'; '4' '7' '2'; '7' '5' '2'}
B =
3×3 cell array
{'2'} {'3'} {'5'}
{'4'} {'7'} {'2'}
{'7'} {'5'} {'2'}
I want to convert it a numeric matrix like as follows:
A =
2 3 5
4 7 2
7 5 2

Réponse acceptée

Stephan
Stephan le 12 Mai 2021
Modifié(e) : Stephan le 12 Mai 2021
B={'2' '3' '5'; '4' '7' '2'; '7' '5' '2'}
B = 3×3 cell array
{'2'} {'3'} {'5'} {'4'} {'7'} {'2'} {'7'} {'5'} {'2'}
C = cellfun(@(x)str2double(x),B)
C = 3×3
2 3 5 4 7 2 7 5 2
  3 commentaires
Stephen23
Stephen23 le 12 Mai 2021
Or, by simply reading the str2double documentation, you can easily have much much more efficient code:
B = {'2','3','5';'4','7','2';'7','5','2'};
M = str2double(B)
M = 3×3
2 3 5 4 7 2 7 5 2
Souarv De
Souarv De le 12 Mai 2021
Woow. That's one is more easier to remember. Thanks to you once again Stephen.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Conversion 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