Hey all, I want to thanks the people who helped me on the binary sequences. Now, I am in a difficult position to figure out a k-ary sequence. A k-ary sequence is one in which every element is an integer from 0,1,...,k-1. For example,{2,2,0,1,1,3,3,3,2,3} is a 4-ary sequence. The elements in 4-ary sequence are randomly spotted. Therefore, I want to print out all the possible combinations. I am new to the matlab, could anyone help me to figure out how to code this?

댓글 수: 3

Guillaume
Guillaume 2014년 11월 30일
It's not clear what the inputs are. You want to generate all the possible combinations of what?
Bear
Bear 2014년 11월 30일
I want to input k value, and generate all the possible combinations of k-ary sequence. Such as the 4-ary sequence above there, there are total 1 zero, 2 ones, 3 twos and 4 threes. But their spots in the sequence will be randomly. I want to print out the all the unique combinations of 4-ary sequence.
Image Analyst
Image Analyst 2014년 11월 30일
Do you want to save a combination that does not appear, but that could appear, for example (using 0,1,2,3) you might have a combination of (1,2,3) but you don't have that in the data, or you might have a combination of (3,2,1,1,2,3,3,2) but you don't.
If you're allowing sequences that don't actually occur in the data, how long can these sequences be? Any length from 1 up to the length of the array, or even longer???

댓글을 달려면 로그인하십시오.

 채택된 답변

Guillaume
Guillaume 2014년 11월 30일

0 개 추천

For reasonably short sequences, use unique and perms (same as for binary sequences):
kseq = [2,2,0,1,1,3,3,3,2,3];
allp = unique(perms(kseq), 'rows')
For long sequences, perms becomes inefficient/unusable. This thread got the code for a more efficient uniqueperms by John D'Errico.

추가 답변 (1개)

Star Strider
Star Strider 2014년 11월 30일

0 개 추천

This seems to do what you want:
k = 4;
s = perms([0:k-1]);

댓글 수: 4

Bear
Bear 2014년 11월 30일
hey, I do not think this is correct. {2,2,0,1,1,3,3,3,2,3} is a 4-ary sequence. and k=4 at this time, the total number of elements in this sequence is equal to (k*(k+1))/2, which is 10, your output is 4 elements total.
Does this do what you want?
len = fix(k*(k+1)/2);
cmbs = nchoosek(len,k);
for k1 = 1:cmbs
seq(k1,:) = randi([0 k-1], 1, len);
end
sequ = unique(seq,'rows');
Bear
Bear 2014년 11월 30일
I think you are close to right, but as the 4-ary sequence above there, there are total 1 zero, 2 ones, 3 twos and 4 threes. But their spots in the sequence will be randomly. I want to print out the all the unique combinations of 4-ary sequence.
Star Strider
Star Strider 2014년 11월 30일
편집: Star Strider 2014년 11월 30일
There may be as many as 4^10 = 1048576 such unique sequences, so get another several reams of paper and a dozen or more ink or toner cartridges first.
I limited my results to 210 to demonstrate that there were no repeats in that number of sequences.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 MATLAB Coder에 대해 자세히 알아보기

질문:

2014년 11월 30일

편집:

2014년 11월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by