Convert matrix of char to numbers

2 次查看(过去 30 天)
I have a big m*n matrix with char elements (the elements are natural numbers). I want to change it to a matrix to work with numbers. Using for-loop and str2num works, but I want to avoid for-loop and a fast and efficient way.
[m,n]=size(Matrix);
for i=1:n
str2num(Matrix(:,i));
end
Any help would be appreciated.
Thanks,

回答(1 个)

Azzi Abdelmalek
Azzi Abdelmalek 2016-3-28
v=['123';'456';'789';'456']
[n,m]=size(v)
w=str2double(mat2cell(v',ones(m,1),n))
  6 个评论
Shima Asaadi
Shima Asaadi 2016-3-28
But I need to look at this cell array as a matrix. I mean that for example v(2,2)=3 , v(2,3)=7, v(3,3)=1 , ...
Thanks
Azzi Abdelmalek
Azzi Abdelmalek 2016-3-28
v={'2' '3' '1'
'4' '3' '7'
'2' '4' '1'}
out=str2double(v)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by