Filter löschen
Filter löschen

Number to matrix

29 Ansichten (letzte 30 Tage)
student
student am 9 Feb. 2012
Bearbeitet: Cedric am 8 Okt. 2013
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 Kommentare
student
student am 9 Feb. 2012
I want to convert it to integers.
As in, 12345 into [ 1 , 2 , 3 , 4 , 5 ]
Andrei Bobrov
Andrei Bobrov am 10 Feb. 2012
:)
fix(rem(TheNumber*10.^-(ceil(log10(TheNumber)):-1:1),1)*10)

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Walter Roberson
Walter Roberson am 9 Feb. 2012
sprintf('%d', TheNumber) - '0'
is the simplest, but not the most efficient.

Benjamin Schwabe
Benjamin Schwabe am 9 Feb. 2012
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 Kommentare
Benjamin Schwabe
Benjamin Schwabe am 9 Feb. 2012
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
Walter Roberson am 10 Feb. 2012
When you are dealing with single digits, subtracting the character '0' is much more efficient than str2double().

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by