How can I merge vector elements into a single number of type double?
이전 댓글 표시
Hello,
How can I transform this vector:
v=[1 2 3]
into this scalar of type double?:
k=123
Thank you,
Lazaros.
채택된 답변
추가 답변 (2개)
x = [1,2,3];
d = x * 10 .^ (numel(x)-1:-1:0).' % dot product
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.
댓글 수: 3
Guillaume
2019년 2월 4일
If you were going to go through strings and back, this would be simpler and faster:
str2num(char(A + '0'))
However, conversion of numbers to/from strings are slow (compared to just doing maths)
Guillaume
2019년 2월 5일
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), ''))
카테고리
도움말 센터 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!