Enumeration of all possible samples with replacement trough base conversion

1 vue (au cours des 30 derniers jours)
Hello everybody,
I would like to write a fast efficient code to enumerate all possible samples with replacement. That is, I need a bijective function between the set of all samples of length k taken from a set of n elements and the subset of natural numbers fast to compute in Matlab.
I can identify every member of A as a number between 0 and so that I can consider every element of as a representation in base n, and I would like to convert it in base using the
base2dec('strn',n)
built-in function of Matlab. The problem I have is the one to convert row vectors containing elements of in strings that could be managed by base2dec.
Indeed, suppose I have generated all the samples with function
X = 0:1:(n-1)
M = VChooseKRO_M_2(X,k)
I would like then to use num2str function to convert every row of M to a string, but for example what I get is
str = num2str(M(8,:))
str = '0 0 0 0 2 1'
while I would like to obtain
str= '000021'
so that then
base2dec(str,3)
ans = 7
is the correct answer.
Could anybody help me or point to a better solution?
  1 commentaire
Athul Prakash
Athul Prakash le 12 Août 2019
Modifié(e) : Athul Prakash le 12 Août 2019
If you have a str = '0 0 0 0 2 1' and want to remove the spaces, just use str(str~=' '). (You would be indexing into str and choosing all characters ~= space)
n = '1 2 3 4';
n_new = n(n~=' ');
Alternatively, you can use a FORMAT specifier in your num2str call itself.
a = num2str([1 2 3 4], '%d') %this does the trick (a='1234' is the output).
However, I can see 2 possible issues with this method of mapping to a decimal integer:
1) N>10 would cause issues.
You can't work with a base more than 10. For example, if you get str = '10 11 12', concatenating to '101112' would clearly give the wrong output.
2) Strings are not ideal for this problem.
Converting integers to strings is not necessary, it would probably be inefficient to do things this way. Please refer to my answer below for another alternative.

Connectez-vous pour commenter.

Réponse acceptée

Athul Prakash
Athul Prakash le 12 Août 2019
Hi Riccardo,
Converting to strings seems inefficient to me (and it not required for this problem either).
Here is an alternative way to get the output:
% I assume all elements in the vector are b/w '0' and 'n-1' inclusive.
N=5;
vec = [1 4 3 1] % In base 5, this is 241.
% now for the solution.
powN = (length(vec)-1): -1: 0; % output: powN = 3 2 1 0 (powers of N for each digit)
powN = N.^powN; % output: powN = 125 25 5 1 (place value of each digit for base 5)
ans = vec*(powN'); % output: ans = 241
Explanation: Generate powers of N in a vector and then dot multiply this to get your answer.
You can combine these statements if you wish to speed up your code. Try:
ans = vec*(N.^((length(vec)-1:-1:0)'));
Hope it Helps!

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by