# How can I merge vector elements into a single number of type double?

조회 수: 1(최근 30일)
Hello,
How can I transform this vector:
v=[1 2 3]
into this scalar of type double?:
k=123
Thank you,
Lazaros.

로그인 to comment.

### 채택된 답변

Guillaume 4 Feb 2019
편집: Guillaume 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].

로그인 to comment.

### 추가 답변(2개)

Jan 4 Feb 2019
편집: Jan 4 Feb 2019
x = [1,2,3];
d = x * 10 .^ (numel(x)-1:-1:0).' % dot product

#### 댓글 수: 0

로그인 to comment.

YT 4 Feb 2019
편집: YT 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.

#### 댓글 수: 3

Guillaume 4 Feb 2019
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)
YT 4 Feb 2019
Looks much better. Thanks, learned something new today. The only downside to this is that, like you stated in your polyval solution, that this also only works correctly for positive integers.
Guillaume 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), ''))

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.

Translated by