Effacer les filtres
Effacer les filtres

Merge array to a number

4 vues (au cours des 30 derniers jours)
Raviteja
Raviteja le 9 Oct 2011
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 ?

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 9 Oct 2011
str2double(sprintf('%d',x))

Plus de réponses (2)

Zhao g-liang
Zhao g-liang le 9 Oct 2011
what about this x=1:5; 10.^(x(end:-1:1)-1)*x'
  1 commentaire
Walter Roberson
Walter Roberson le 9 Oct 2011
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))

Connectez-vous pour commenter.


Walter Roberson
Walter Roberson le 9 Oct 2011
str2double(num2str(x))
  1 commentaire
Raviteja
Raviteja le 9 Oct 2011
Is this working?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by