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

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

채택된 답변

Guillaume
Guillaume 2019년 2월 4일
편집: Guillaume 2019년 2월 4일
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].

추가 답변 (2개)

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

YT
YT 2019년 2월 4일
편집: YT 2019년 2월 4일
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
YT
YT 2019년 2월 4일
편집: YT 2019년 2월 4일
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
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), ''))

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by