How do I vectorize each digit of an integer and store it as an array?

30 vues (au cours des 30 derniers jours)
Srishti Saha
Srishti Saha le 8 Mai 2018
Modifié(e) : Stephen23 le 8 Mai 2018
I have to convert an integer into a vector of all its digits. Example: 100---> [1 0 0]
function ans = int2vec(x)
t=regexp(num2str(x),'\d','match');
[];
arrayfun(@(i) [ans,str2num(t{i})],1:length(t));
end
Though this worked for me, is there a better solution.

Réponse acceptée

Stephen23
Stephen23 le 8 Mai 2018
Modifié(e) : Stephen23 le 8 Mai 2018
>> num2str(100)-'0'
ans =
1 0 0
If you want all digits, regardless of position and role (e.g. exponent):
>> s = num2str(100);
>> s(isstrprop(s,'digit'))-'0'
ans =
1 0 0

Plus de réponses (1)

Stalin Samuel
Stalin Samuel le 8 Mai 2018

Catégories

En savoir plus sur Data Type Conversion 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