|
|
Re: nchoosek all pairwise combinations
Posted:
Nov 27, 2012 4:05 AM
|
|
On 11/27/2012 2:40 AM, Nasser M. Abbasi wrote:
> > can't you just make a loop? > > --------------------- > clear all > A = ['a', 'b', 'c', 'd','e']; > N = size(A,2); > B = cell(N); > > for i = 1:N > for j = 1:N > B(i,j) = {strcat(A(j),A(i))}; > end > end > -------------------- > > B = > > 'aa' 'ba' 'ca' 'da' 'ea' > 'ab' 'bb' 'cb' 'db' 'eb' > 'ac' 'bc' 'cc' 'dc' 'ec' > 'ad' 'bd' 'cd' 'dd' 'ed' > 'ae' 'be' 'ce' 'de' 'ee' >
Another option
-------------------------- clear all A = ['a', 'b', 'c', 'd','e']; L = 1:size(A,2); B = A(allcomb(L,L)); Z = arrayfun(@(i) {strcat(B(i,1),B(i,2))},1:length(B)); reshape(Z,5,5) --------------------------
which gives the same answer as above
Used allcomb() in http://www.mathworks.com/matlabcentral/fileexchange/10064
--Nasser
|
|