How can I merge vector elements into a single number of type double?
6 views (last 30 days)
Show older comments
Lazaros Ioakeimidis on 4 Feb 2019
Commented: Guillaume on 5 Feb 2019
How can I transform this vector:
v=[1 2 3]
into this scalar of type double?:
Guillaume on 4 Feb 2019
Edited: Guillaume on 4 Feb 2019
k = polyval(v, 10)
is probably the easiest.
This assume of course that each element of v is an integer in the range [0-9].
More Answers (2)
Jan on 4 Feb 2019
Edited: Jan on 4 Feb 2019
x = [1,2,3];
d = x * 10 .^ (numel(x)-1:-1:0).' % dot product
YT on 4 Feb 2019
Edited: YT on 4 Feb 2019
Something like this?
A = [1 2 3];
joined = str2num(strjoin(num2cell(num2str(A(:))),''));
%>> joined = 123 (type double)
There are probably some more elegant solutions, but this is what I came up with for now.
Guillaume on 5 Feb 2019
Indeed but that is most likely the case I assume.
A simpler, yet more generic solution, using number->string->number conversion:
str2num(strjoin(compose('%d', A), ''))
Find more on Logical 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!