Convert matrix of char to numbers

1 view (last 30 days)
Shima Asaadi
Shima Asaadi on 28 Mar 2016
Commented: Azzi Abdelmalek on 28 Mar 2016
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,

Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 28 Mar 2016
v=['123';'456';'789';'456']
[n,m]=size(v)
w=str2double(mat2cell(v',ones(m,1),n))
  6 Comments
Shima Asaadi
Shima Asaadi on 28 Mar 2016
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 on 28 Mar 2016
v={'2' '3' '1'
'4' '3' '7'
'2' '4' '1'}
out=str2double(v)

Sign in to comment.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!