0

I want to create all pattern combination which possible occur

For example, I have three ball and I want to pick 3 times. All possible is 27 events but I want to create all possible event in array like this [1 1 1; 1 1 2; 1 1 3; 1 2 1 ;....]

Dose anyone can help me to write m-file in matlab program, please?

OmG
  • 18,337
  • 10
  • 57
  • 90

2 Answers2

1

This can be done very easily with base conversion.

If the number of balls does not exceed 10

M = 3; % Number of balls. Must not exceed 10
N = 3; % Number of draws
result = dec2base(0:M^N-1, M)-'0'+1;

Note that dec2base ouputs chars, not numbers, and hence the -'0' part. Characters in arithmetic operations behave like their corresponding ASCII codes. So subtracting '0' transforms characters '0', '1', ..., '9' into the corresponding numbers.

With this approach M cannot exceed 10 because then dec2bin would output '0', '1', ..., '9', 'A', 'B' ... and the character arithmetic would not give the correct result for 'A', 'B', ... But this can be easily solved as follows.

For number of balls up to 36

M = 12; % Number of balls. Must not exceed 36
N = 2; % Number of draws
result = dec2base(0:M^N-1, M)-'0'+1; % same as before
result = result - ('A'-'9'-1)*(result>10); % correction

The new line simply corrects the results for 'A', 'B', ... by subtracting 'A'-'9'-1, to compensate the fact that '9' and 'A' do not have consecutive ASCII codes.

With this approach M cannot exceed 36 because of dec2base restrictions.

Community
  • 1
  • 1
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
  • 1
    @LeanderMoesinger Characters in arithmetic operations behave like their corresponding ASCII codes. So for example `'2'` is the same as `50` and `'0'` is `48`, and thus `'2'-'0'` gives `2`. In general, subtracting `'0'` transforms digits characters into the corresponding digit. I have added an explanation – Luis Mendo Jul 18 '17 at 15:29
0

You can create the result like this for the specific case:

  firstCol = [ones(9,1);2*ones(9,1);3*ones(9,1)];
  secondCol = repeat([ones(3,1);2*ones(3,1);3*ones(3,1)],1,3);
  thirdCol = repeat([1;2;3],1,9);
  result = [firstCol secondCol thirdCol];

First, repeat 9 times 1,2, and 3 for first column. then repeat each of them 3 times for the second column and choose the third column once for each item. Indeed, this generate the all possible choices for each location.

How? If you suppose the first element is 1, you have 3 choice for the second place, and 3 choice for the third place. Hence, you have 9 possible option when the first place is 1. Also, fix the second place, and analyze this. You can generalize this for 2 and 3. The above code, try to generate possibilities based on this explanation.

In the above, ones generate a matrix which all elements are 1 with the specified size and repeat function repeats the specified matrix in the specified size and dimension. You can check the documentation to know more about them.

Hence, You can generalize it for n like the following:

 n = 10;
 result = zeros(3^n,3);
 for idx = 1:n
     result(:,idx) = repeat([ones(3^(n-idx),1);2*ones(3^(n-idx),1);3*ones(3^(n-idx),1)],1,3^(idx-1));
 end
OmG
  • 18,337
  • 10
  • 57
  • 90
  • Thank you for your answer. If I change to question to another question like this. I have three balls , I want to pick n times. So the possible event is 3^n. How can I create all event in the array? – keng Surawit Jul 18 '17 at 12:18
  • @kengSurawit see the updated post. – OmG Jul 18 '17 at 12:35
  • I don't have the repeat command in my Matlab 2013. Do you have any code to generalize this question? – keng Surawit Jul 19 '17 at 05:52