Number to matrix
18 ビュー (過去 30 日間)
古いコメントを表示
Hello!
I am very new to MatLab. I am wondering, what is the simplest way to convert a 5-digit number into a matrix of those 5 digits.
Thanks,
3 件のコメント
回答 (2 件)
Walter Roberson
2012 年 2 月 9 日
sprintf('%d', TheNumber) - '0'
is the simplest, but not the most efficient.
0 件のコメント
Benjamin Schwabe
2012 年 2 月 9 日
You can do that by simply using the following code:
p = 12345; % define your number
pstr=num2str(p);
n=length(pstr);
v=zeros(n,1);
for k=1:n
v(k)=str2double(pstr(k));
end
It will work for all integer numbers. For non Integer numbers, the "." will be transformed into 'NaN'.
2 件のコメント
Benjamin Schwabe
2012 年 2 月 9 日
Maybe some more details:
I convert the number to a string with num2str, then I take each character of the string and transfort it back to a number.
If you want to have a row vector instead of a column, just replace
v=zeros(n,1); by v=zeros(1,n);
Walter Roberson
2012 年 2 月 10 日
When you are dealing with single digits, subtracting the character '0' is much more efficient than str2double().
参考
カテゴリ
Help Center および File Exchange で Numeric Types についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!