Converting Cell matrix to a Numeric Matrix

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

 채택된 답변

Stephan
Stephan 2021년 5월 12일
편집: Stephan 2021년 5월 12일
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

Souarv De
Souarv De 2021년 5월 12일
Thanks Stephan. It works.
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 2021년 5월 12일
Woow. That's one is more easier to remember. Thanks to you once again Stephen.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

릴리스

R2018a

질문:

2021년 5월 12일

댓글:

2021년 5월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by