i need to convert a string to a num without using the "str2num" function but in the final answer i have some undesired spaces. how can i remove them?

2 Ansichten (letzte 30 Tage)
st = '23487';
x = double(st);
y = x-48;
y = 2 3 4 8 7
i want this in "normal" format of "23487"...
  2 Kommentare
Image Analyst
Image Analyst am 24 Nov. 2012
Bearbeitet: Image Analyst am 24 Nov. 2012
Is this a Homework question? If so, we'll give hints, but not the answer.
Matt Fig
Matt Fig am 24 Nov. 2012
Bearbeitet: Matt Fig am 24 Nov. 2012
^Or so one would hope...
Here is how to do it without the str2num function:
N = str2double('1234')

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 24 Nov. 2012
Bearbeitet: Azzi Abdelmalek am 24 Nov. 2012
st = '14523'
x = double(st)
y = sum((x-48).*10.^(numel(st)-1:-1:0))
  1 Kommentar
Zaza
Zaza am 24 Nov. 2012
of course! represent '23487' as 2*10^4+3*10^3+4*10^2+8*10^1+7*10^0... i should've think better on this one. thanks Azzi :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Andrei Bobrov
Andrei Bobrov am 24 Nov. 2012
Bearbeitet: Andrei Bobrov am 26 Nov. 2012
out = (st-'0')*10.^(numel(st)-1:-1:0)';
or from Cody
out = polyval(st-'0',10);

Kategorien

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

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by