MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

### Discover what MATLAB® can do for your career.

 Subject: combination lock From: Alex Date: 23 Jun, 2011 14:28:04 Message: 1 of 2 I have a large martix with varying number of rows and columns. I want to add all the possible cominations together. I.e all the cominations on a lock: say the lock has three columns and 3 rows: (matrix is called array) 1 2 3 4 5 6 7 8 9 I have found the function shared on matlab allcomb which can do this if the rows of the array are known. Each row can be refered to as array (ii,:) in a for loop The function allcomb is used in the following fashion allcomb([1 2 3],[4 5 6],[7 8 9]) Instead I want to put allcomb(array(ii,:),array(ii+1,:),array(ii+2,:)) I.e dynamic values going to the function. This works fine, but what if i had a huge matrix. I dont want to have to put in every bit into the function, so can I send it all the rows of the matrix in a for loop or something, because otherwise this would have to be changed if the matrix changed size? P.S I then want to sum all the rows of combinations. Just saying this incase it affects the method I should use to do this. Thanks everyone
 Subject: combination lock From: Roger Stafford Date: 24 Jun, 2011 03:42:04 Message: 2 of 2 "Alex" wrote in message ... > I have a large martix with varying number of rows and columns. I want to add all the possible cominations together. I.e all the cominations on a lock: > say the lock has three columns and 3 rows: (matrix is called array) > 1 2 3 > 4 5 6 > 7 8 9 > > > I have found the function shared on matlab allcomb which can do this if the rows of the array are known. > Each row can be refered to as array (ii,:) in a for loop > > The function allcomb is used in the following fashion > > allcomb([1 2 3],[4 5 6],[7 8 9]) > Instead I want to put > allcomb(array(ii,:),array(ii+1,:),array(ii+2,:)) > > I.e dynamic values going to the function. This works fine, > but what if i had a huge matrix. I dont want to have to put in every bit into the function, so can I send it all the rows of the matrix in a for loop or something, because otherwise this would have to be changed if the matrix changed size? > > P.S I then want to sum all the rows of combinations. Just saying this incase it affects the method I should use to do this. > > Thanks everyone - - - - - - - - - - - - -   Denote by A your matrix in which we are to select every possible combination of one element from each of its rows.  [m,n] = size(A);  AT = A.';  B = zeros(n^m,m);  B(1:n,m) = AT(:,m);  p = n;  for k = m-1:-1:1   [I,J] = meshgrid(1:n,1:p);   p = p*n;   B(1:p,k:m) = [AT(I,k),B(J,k+1:m)];  end Then the n^m by m array B is the desired result. Each row has one of the possible combinations of a single element chosen from each row of A.   I doubt if its speed can compare with that of 'allcomb' but at least it will get you there.   Note: Be careful with your selection of sizes. The quantity n^m can quickly become very large. Roger Stafford