help-octave
[Top][All Lists]

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

 From: Juan Pablo Carbajal Subject: Re: how to make a matrix with all combinations of digits efficiently Date: Fri, 29 Dec 2017 19:13:47 +0100

```> 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

another one

d = dec2digit (707, 16);
n = length (d);
d * 16.^[(n-1):-1:0].'
ans =  707

another one

num = randi (10e10);
dec2digit (num)   # this one should be one of the ones you need.

If you are calling this from a loop, then you do not have memory
problems. You cna also use batches of numbers that follow a memory
restriction, i.e. work with batches of 1MB, then 1e6/8/10  is more or
less the number of rows of the matrix for that batch.

```