Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

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


Math Forum » Discussions » Software » comp.soft-sys.matlab

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

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Nasser Abbasi

Posts: 5,705
Registered: 2/7/05
Re: nchoosek all pairwise combinations
Posted: Nov 27, 2012 4:05 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.