http://www.mathworks.com/matlabcentral/newsreader/view_thread/309652
MATLAB Central Newsreader  combination lock
Feed for thread: combination lock
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Thu, 23 Jun 2011 14:28:04 +0000
combination lock
http://www.mathworks.com/matlabcentral/newsreader/view_thread/309652#842609
Alex
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:<br>
say the lock has three columns and 3 rows: (matrix is called array)<br>
1 2 3<br>
4 5 6<br>
7 8 9<br>
<br>
<br>
I have found the function shared on matlab allcomb which can do this if the rows of the array are known. <br>
Each row can be refered to as array (ii,:) in a for loop<br>
<br>
The function allcomb is used in the following fashion<br>
<br>
allcomb([1 2 3],[4 5 6],[7 8 9])<br>
Instead I want to put<br>
allcomb(array(ii,:),array(ii+1,:),array(ii+2,:))<br>
<br>
I.e dynamic values going to the function. This works fine,<br>
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?<br>
<br>
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.<br>
<br>
Thanks everyone

Fri, 24 Jun 2011 03:42:04 +0000
Re: combination lock
http://www.mathworks.com/matlabcentral/newsreader/view_thread/309652#842707
Roger Stafford
"Alex" wrote in message <itvihk$hdn$1@newscl01ah.mathworks.com>...<br>
> 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:<br>
> say the lock has three columns and 3 rows: (matrix is called array)<br>
> 1 2 3<br>
> 4 5 6<br>
> 7 8 9<br>
> <br>
> <br>
> I have found the function shared on matlab allcomb which can do this if the rows of the array are known. <br>
> Each row can be refered to as array (ii,:) in a for loop<br>
> <br>
> The function allcomb is used in the following fashion<br>
> <br>
> allcomb([1 2 3],[4 5 6],[7 8 9])<br>
> Instead I want to put<br>
> allcomb(array(ii,:),array(ii+1,:),array(ii+2,:))<br>
> <br>
> I.e dynamic values going to the function. This works fine,<br>
> 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?<br>
> <br>
> 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.<br>
> <br>
> Thanks everyone<br>
            <br>
Denote by A your matrix in which we are to select every possible combination of one element from each of its rows.<br>
<br>
[m,n] = size(A);<br>
AT = A.';<br>
B = zeros(n^m,m);<br>
B(1:n,m) = AT(:,m);<br>
p = n;<br>
for k = m1:1:1<br>
[I,J] = meshgrid(1:n,1:p);<br>
p = p*n;<br>
B(1:p,k:m) = [AT(I,k),B(J,k+1:m)];<br>
end<br>
<br>
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.<br>
<br>
I doubt if its speed can compare with that of 'allcomb' but at least it will get you there.<br>
<br>
Note: Be careful with your selection of sizes. The quantity n^m can quickly become very large.<br>
<br>
Roger Stafford