MATLAB Answers

0

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

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.

  댓글 수: 0

로그인 to comment.

답변 수: 2

Stephen Cobeldick 님의 답변 8 May 2018
Stephen Cobeldick 님이 편집함. 8 May 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

  댓글 수: 0

로그인 to comment.




Translated by