help-octave
[Top][All Lists]

## Re: how to make a matrix with all combinations of digits efficiently

 From: Jean Dubois Subject: Re: how to make a matrix with all combinations of digits efficiently Date: Sat, 30 Dec 2017 17:56:23 +0100

```2017-12-29 19:13 GMT+01:00 Juan Pablo Carbajal <address@hidden>:
>> in fact I need decomp(10,10)
>
> The issue with memory or index size are rather unavoidable. Do you
> really need all this?
> What is your intended use of counter? I am pretty sure you do not neet
> to allocate nor have simultaneously such an enormous amount of
> numbers.
> Note that my solution works even for a single number "i", that means
> you could call the function to decompose a given number everytime you
> need a results, i.e.
>
> function digits = dec2digit (num, base = 10)
> nc      = ceil (log (num) / log (base));
> digits = mod( floor (num ./ base.^[(nc-1):-1:0]), base);
> endfunction
>
> example
>
> dec2digit (537)
> ans =
>
>    5   3   7
Dear Juan,
I just saw that dec2digit(100) produces 0 0 in stead of 1 0 0, so I
changed the function as follows
function digits = dec2digit (num, nc, base = 10)
%nc      = ceil (log (num) / log (base));
digits=mod(floor(num./base.^[(nc-1):-1:0]), base);

kind regards

```