How do you create the number as mentioned below?
Show older comments
Accepted Answer
More Answers (3)
Marc Jakobi
on 8 Oct 2016
I won't tell you how to do it since this sounds like homework, but here are some functions that may help you.
str2double() % convert string to double
num2str() % convert double to string
to combine two strings:
a = 'a';
b = 'b';
ab = [a, b];
returns
'ab'
and
ab(2)
returns
ab(2) = 'b'
Star Strider
on 8 Oct 2016
Without using strings at all, this works strictly numerically:
M1 = repmat([0:9], 10, 1);
M2 = repmat([0:9]', 1, 10);
M3 = cat(3,M1, M2);
M4 = reshape(M3, [], 2);
M5 = reshape(M4', 1, []);
Out = [1:9 M5(21:end)]
Walter Roberson
on 9 Oct 2016
Edited: Walter Roberson
on 9 Oct 2016
For amusement, a Maple version of the general solution, using direct numeric calculations without creating the intermediate values.
H := proc (M)
local min_numdig, contrib_from_min, leftover, dp1, base_number, dig, dig_offset;
min_numdig := floor(((1/9)*ln(10)+LambertW((1/90)*ln(10)*(9*M-1)*10^(8/9)))/ln(10));
dp1 := min_numdig+1;
contrib_from_min := (1/90)*10^dp1*(9*min_numdig-1)+1/9;
leftover := M-contrib_from_min;
if leftover = 0
then
dig := "9"
else
base_number := 10^min_numdig-1+floor(leftover/dp1);
dig_offset := `mod`(leftover, dp1);
if dig_offset = 0
then
dig_offset := dp1
else
base_number := base_number+1
end if;
dig := sprintf("%d", base_number)[dig_offset]
end if;
dig
end proc;
Note: this fails on 0 due to both a Maple technicality and a logic bug. The mathematics of it is presented without proof.
No MATLAB version of this will be provided, as the question is obviously a homework question.
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!