how to convert a single row matrix into a number (double)

8 views (last 30 days)
how to convert a single row matrix into a number (double) for exemple: [1 2 3 4 5] into 12345

Accepted Answer

Rik
Rik on 6 Feb 2018
a=[1 2 3 4 5];
b=str2double(sprintf('%d',round(a)));
Another method would be to use positional information:
a=[1 2 3 4 5];
b=sum(a.*10.^((length(a)-1):-1:0));
This second method will often work better, but if values larger than 9 are present in a, this will not return the result you might mean.
  1 Comment
Rik
Rik on 6 Feb 2018
You might be able to correct the powers of 10 vector with cumsum(a>10), or more directly with floor(log10(a))

Sign in to comment.

More Answers (1)

Adam
Adam on 6 Feb 2018
Edited: Adam on 6 Feb 2018
a = [1 2 3 4 5];
str2double( strrep( num2str( a ), ' ', '' ) )
works, but I'm sure there are more elegant or robust ways of doing it.

Categories

Find more on Numeric Types 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!