필터 지우기
필터 지우기

Merge array to a number

조회 수: 4 (최근 30일)
Raviteja
Raviteja 2011년 10월 9일
I want to merge an array to a number. i.e., if
>>x=1:5;
do some operation on x and get the result like
>>y=12345
for that I have written this code...
x=1:5;
y=0;
for i=1:length(x)
y=y*10+x(i);
end
Is there any short way to do this ?

채택된 답변

Andrei Bobrov
Andrei Bobrov 2011년 10월 9일
str2double(sprintf('%d',x))

추가 답변 (2개)

Zhao g-liang
Zhao g-liang 2011년 10월 9일
what about this x=1:5; 10.^(x(end:-1:1)-1)*x'
  댓글 수: 1
Walter Roberson
Walter Roberson 2011년 10월 9일
Better to use
10.^(length(x)-1:1:0)*x.'
If the poster could rely upon x being consecutive digits then the poster could just use '0' + (x(1):x(end))

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


Walter Roberson
Walter Roberson 2011년 10월 9일
str2double(num2str(x))
  댓글 수: 1
Raviteja
Raviteja 2011년 10월 9일
Is this working?

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

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by