Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: nchoosek all pairwise combinations
Replies: 10   Last Post: Nov 27, 2012 7:46 AM

 Messages: [ Previous | Next ]
 Nasser Abbasi Posts: 6,677 Registered: 2/7/05
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

Date Subject Author
11/26/12 Jerry
11/27/12 Nasser Abbasi
11/27/12 Nasser Abbasi
11/27/12 Nasser Abbasi
11/27/12 Jos
11/27/12 Nasser Abbasi
11/27/12 Jos
11/27/12 Nasser Abbasi
11/27/12 Jos
11/27/12 Nasser Abbasi
11/27/12 Jos