Discover MakerZone

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

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
nchoosek all pairwise combinations

Subject: nchoosek all pairwise combinations

From: Jerry

Date: 27 Nov, 2012 04:12:21

Message: 1 of 11

Hello,

There is a vector

A=['a', 'b', 'c', 'd','e']

I would like to make a square matrix of all paiwise interactions of this vector:


a b c d e
a aa ba ca da ea
b ab bb cb db eb
c ac bc cc dc ec
d ad bd cd dd ed
e ae bd ce de ee

However, nchoosek allow me to create only half of this matrix. Is there any function to make all these possible interactions within "A"?

Thanks,
Jerry

Subject: nchoosek all pairwise combinations

From: Nasser M. Abbasi

Date: 27 Nov, 2012 06:16:14

Message: 2 of 11

On 11/26/2012 10:12 PM, Jerry wrote:
> Hello,
>
> There is a vector
>
> A=['a', 'b', 'c', 'd','e']
>
> I would like to make a square matrix of all paiwise interactions of this vector:
>
>
> a b c d e
> a aa ba ca da ea
> b ab bb cb db eb
> c ac bc cc dc ec
> d ad bd cd dd ed
> e ae bd ce de ee
>
> However, nchoosek allow me to create only half of this matrix.
>Is there any function to make all these possible interactions within "A"?
>

Much easier to do this when your 'a','b', etc... are numerics, not
chars or strings.

What is the reason of using strings for this?
--Nasser

Subject: nchoosek all pairwise combinations

From: Nasser M. Abbasi

Date: 27 Nov, 2012 08:40:07

Message: 3 of 11

On 11/26/2012 10:12 PM, Jerry wrote:
> Hello,
>
> There is a vector
>
> A=['a', 'b', 'c', 'd','e']
>
> I would like to make a square matrix of all paiwise interactions of this vector:
>
>
> a b c d e
> a aa ba ca da ea
> b ab bb cb db eb
> c ac bc cc dc ec
> d ad bd cd dd ed
> e ae bd ce de ee
>
> However, nchoosek allow me to create only half of this matrix. Is there any function
>to make all these possible interactions within "A"?
>

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'


--Nasser

Subject: nchoosek all pairwise combinations

From: Nasser M. Abbasi

Date: 27 Nov, 2012 09:05:33

Message: 4 of 11

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

Subject: nchoosek all pairwise combinations

From: Jos (10584)

Date: 27 Nov, 2012 09:40:08

Message: 5 of 11

"Jerry " <jerrycholo@gmail.com> wrote in message <k91ej5$bp5$1@newscl01ah.mathworks.com>...
> Hello,
>
> There is a vector
>
> A=['a', 'b', 'c', 'd','e']
>
> I would like to make a square matrix of all paiwise interactions of this vector:
>
>
> a b c d e
> a aa ba ca da ea
> b ab bb cb db eb
> c ac bc cc dc ec
> d ad bd cd dd ed
> e ae bd ce de ee
>
> However, nchoosek allow me to create only half of this matrix. Is there any function to make all these possible interactions within "A"?
>
> Thanks,
> Jerry

There are many ways to achieve such a matrix, but you should think about how you would store your data. Do you really want a character matrix?

A very easy way to obtain the interactions would be using NDGRID

A = 'abcde'
[B1, B2] = ndgrid(A)

which stores the interactions in two separate yet related matrices ...
It all depends on what you want to do with the result!

~ Jos

Subject: nchoosek all pairwise combinations

From: Nasser M. Abbasi

Date: 27 Nov, 2012 09:53:52

Message: 6 of 11

On 11/27/2012 3:40 AM, Jos (10584) wrote:

> A very easy way to obtain the interactions would be using NDGRID
>
> A = 'abcde'
> [B1, B2] = ndgrid(A)
>
> which stores the interactions in two separate yet related matrices ...
> It all depends on what you want to do with the result!
>
> ~ Jos
>

Yes, good, but the problem is that the result are
all one long string now (in B1 and B2) and there will need to be much more
work done to go from those to the final result the OP wanted,
which is a cell matrix of only 2 chars/per string in each entry.

--Nasser

Subject: nchoosek all pairwise combinations

From: Jos (10584)

Date: 27 Nov, 2012 10:29:08

Message: 7 of 11

"Nasser M. Abbasi" <nma@12000.org> wrote in message <k922jf$sn9$1@speranza.aioe.org>...
> On 11/27/2012 3:40 AM, Jos (10584) wrote:
>
> > A very easy way to obtain the interactions would be using NDGRID
> >
> > A = 'abcde'
> > [B1, B2] = ndgrid(A)
> >
> > which stores the interactions in two separate yet related matrices ...
> > It all depends on what you want to do with the result!
> >
> > ~ Jos
> >
>
> Yes, good, but the problem is that the result are
> all one long string now (in B1 and B2) and there will need to be much more
> work done to go from those to the final result the OP wanted,
> which is a cell matrix of only 2 chars/per string in each entry.
>
> --Nasser
>
>

I did not see the word cell matrix in the OP message ...

but "much more work" is rather overstated:

A = 'abcde'
[B1, B2] = ndgrid(A)
M = reshape(cellstr([B1(:) B2(:)]),5,5)

~ Jos

Subject: nchoosek all pairwise combinations

From: Nasser M. Abbasi

Date: 27 Nov, 2012 10:32:46

Message: 8 of 11

On 11/27/2012 4:29 AM, Jos (10584) wrote:

>
> I did not see the word cell matrix in the OP message ...
>

Well, they are characters and need to be in a 'matrix',
so I assumed it has to be a cell array.


> but "much more work" is rather overstated:
>
> A = 'abcde'
> [B1, B2] = ndgrid(A)
> M = reshape(cellstr([B1(:) B2(:)]),5,5)
>

Nice. I did not know about cellstr. That is definitly
better. 2 lines shorter than mine :)

--Nasser

Subject: nchoosek all pairwise combinations

From: Jos (10584)

Date: 27 Nov, 2012 10:37:08

Message: 9 of 11

"Nasser M. Abbasi" <nma@12000.org> wrote in message
> Nice. I did not know about cellstr. That is definitly
> better. 2 lines shorter than mine :)

... but your code uses my function ALLCOMB which gives you lots of credits ;-)

~ Jos

Subject: nchoosek all pairwise combinations

From: Nasser M. Abbasi

Date: 27 Nov, 2012 10:41:49

Message: 10 of 11

On 11/27/2012 4:37 AM, Jos (10584) wrote:
> "Nasser M. Abbasi" <nma@12000.org> wrote in message
>> Nice. I did not know about cellstr. That is definitly
>> better. 2 lines shorter than mine :)
>
> ... but your code uses my function ALLCOMB which gives you lots of credits ;-)
>

I had harder time since I did not know about cellstr.

Using cellstr, now it is like this (with the use of allcomb (nice function))

------------------------------
A=['a', 'b', 'c', 'd','e']; N=size(A,2);
reshape(cellstr(A(allcomb(1:N,1:N))),N,N)
--------------------------------

     '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'

I dont know how I overlooked cellstr. Very useful.


--Nasser

Subject: nchoosek all pairwise combinations

From: Jos (10584)

Date: 27 Nov, 2012 12:46:10

Message: 11 of 11

"Nasser M. Abbasi" <nma@12000.org> wrote in message <k925db$3ms$1@speranza.aioe.org>...
> On 11/27/2012 4:37 AM, Jos (10584) wrote:
> > "Nasser M. Abbasi" <nma@12000.org> wrote in message
> >> Nice. I did not know about cellstr. That is definitly
> >> better. 2 lines shorter than mine :)
> >
> > ... but your code uses my function ALLCOMB which gives you lots of credits ;-)
> >
>
> I had harder time since I did not know about cellstr.
>
> Using cellstr, now it is like this (with the use of allcomb (nice function))
>
> ------------------------------
> A=['a', 'b', 'c', 'd','e']; N=size(A,2);
> reshape(cellstr(A(allcomb(1:N,1:N))),N,N)
> --------------------------------
>
> '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'
>
> I dont know how I overlooked cellstr. Very useful.
>
>
> --Nasser
>
>

Note that ALLCOMB and its sibling COMBN are "clever" wrappers around NDGRID.

~ Jos

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us