Enumeration of all possible samples with replacement trough base conversion

1 回表示 (過去 30 日間)
Riccardo
Riccardo 2019 年 8 月 7 日
コメント済み: Bruno Luong 2019 年 8 月 12 日
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 件のコメント
Athul Prakash
Athul Prakash 2019 年 8 月 12 日
編集済み: Athul Prakash 2019 年 8 月 12 日
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.

サインインしてコメントする。

採用された回答

Athul Prakash
Athul Prakash 2019 年 8 月 12 日
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!

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by