# Converting Cell matrix to a Numeric Matrix

5 visualizzazioni (ultimi 30 giorni)
Souarv De il 12 Mag 2021
Commentato: Souarv De il 12 Mag 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
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Stephan il 12 Mag 2021
Modificato: Stephan il 12 Mag 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 CommentiMostra 1 commento meno recenteNascondi 1 commento meno recente
Stephen23 il 12 Mag 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 il 12 Mag 2021
Woow. That's one is more easier to remember. Thanks to you once again Stephen.

Accedi per commentare.

### Categorie

Scopri di più su Data Type Conversion in Help Center e File Exchange

R2018a

### Community Treasure Hunt

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

Start Hunting!

Translated by