MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
Asked by Zaza on 4 Jan 2013

for ex.:

n1 = 123; n2 = 12; (n1>=n2)

n2 is a sub number of n1

i can't make any use of strings or arrays

*No products are associated with this question.*

Answer by Sean de Wolski on 4 Jan 2013

Accepted answer

Here is a hint:

a = 131245; b = 12; op = @(x)hidden_for_you_to_figure_out(x); pa = op(a); pb = op(b); isASub = false; %guilty until proven innocent for ii = pb:pa amii = mod(a,(10^ii)); if amii == b isASub = true; return; else for jj = 1:(ii-pb) afjj = floor(amii./(10^jj)); if b == afjj isASub = true; return; end end end end

All you have to do it figure out the contents of `op()`.

Answer by Azzi Abdelmalek on 4 Jan 2013

Edited by Azzi Abdelmalek on 4 Jan 2013

n1=12456; n2=245 a=num2str(n1); b=num2str(n2); c=all(ismember(b,a))

if c is equal to 1 that means n2 is a sub number of n1

Show 3 older comments

Azzi Abdelmalek on 4 Jan 2013

I will give you hints:

a=124 ;a1=1, a2=2, a3=4

to find a3:

a3=fix(a/10^2); %(an=fix(a/10^(n-1))

In your previous question the number of digit in an integer number is

int=-123456789 digit_number=max(ceil(log10(abs(int))),1)

Answer by Walter Roberson on 4 Jan 2013

Create a function that calculates the decimal expansion of a number in *reverse* order. mod() 10 gives the next digit to be output, integer division by 10, if the result is non-zero keep going.

Apply that to both numbers.

Now match the *reversed* n2 to the *reversed* n1.

## 7 Comments

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/57972#comment_120667

What is the class of n1 and n2? Strings, number??? What does this say:

I'm not sure you can do it if you can use strings or arrays. Is a scalar (single number but not an array of 2 or more number) allowed? Also define subnumber. Is 2 a subnumber of 4 because it's a factor of 4? Or do the digits have to be in order in the larger number, like 22 and 42 are subnumbers of 4422, even though they may not be integer factors of it.

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/57972#comment_120674

the class of n1 and n2 is double

scalar is allowed

the digits have to be in order in the larger number, like 22 and 42 are subnumbers of 4422, even though they may not be integer factors of it.

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/57972#comment_120681

Not using strings, this is not trivial (at least I haven't come up with a trivial way to do it yet, maybe a conversion to binary and comparison? or dynamic programming with

mod,log10?).Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/57972#comment_120686

i suppose it's connected somehow with mod fun.

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/57972#comment_120692

suppose i have

n1 = 12345;

how can i omit the first (in the highest power of 10) digit to get

n1 = 2345;

and so on

in this way if n2 is a sub number of n1 it'll match...

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/57972#comment_120694

The highest power of 10 correspond to 1 not 5

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/57972#comment_120696

check this

then

and so on