Thread Subject: matrix

Subject: matrix

From: jack j

Date: 1 Oct, 2009 18:54:02

Message: 1 of 11

I have a matrix M=[1 1 0 0
                           1 0 0 0
                           0 0 1 1
                           0 0 1 0]
If n=[3 1 2], I've to generate a matrix s=[(3,3) (3,1) (3,2)
                                                            (1,3) (1,1) (1,2)
                                                             (2,3) (2,1) (2,2)]

Expected result should be s=[1 0 0
                                          0 1 1
                                          0 1 0]
Can anyone help me?

Subject: matrix

From: Matt Fig

Date: 1 Oct, 2009 19:25:19

Message: 2 of 11

One approach:

N = npermutek(n,2) % On the File Exchange
Newmat = reshape(M(sub2ind(size(M),N(:,1),N(:,2))),3,3)


http://www.mathworks.com/matlabcentral/fileexchange/11462

Subject: matrix

From: jack j

Date: 2 Oct, 2009 05:26:02

Message: 3 of 11

Thanks Matt
Any other way to do it????

"Matt Fig" <spamanon@yahoo.com> wrote in message <ha2vmv$plh$1@fred.mathworks.com>...
> One approach:
>
> N = npermutek(n,2) % On the File Exchange
> Newmat = reshape(M(sub2ind(size(M),N(:,1),N(:,2))),3,3)
>
>
> http://www.mathworks.com/matlabcentral/fileexchange/11462

Subject: matrix

From: Stefano

Date: 2 Oct, 2009 06:28:02

Message: 4 of 11

A double for loop?


"jack j" <12mailjack@gmail.com> wrote in message <ha42ta$dq7$1@fred.mathworks.com>...
> Thanks Matt
> Any other way to do it????
>
> "Matt Fig" <spamanon@yahoo.com> wrote in message <ha2vmv$plh$1@fred.mathworks.com>...
> > One approach:
> >
> > N = npermutek(n,2) % On the File Exchange
> > Newmat = reshape(M(sub2ind(size(M),N(:,1),N(:,2))),3,3)
> >
> >
> > http://www.mathworks.com/matlabcentral/fileexchange/11462

Subject: matrix

From: Bruno Luong

Date: 2 Oct, 2009 06:46:03

Message: 5 of 11

"jack j" <12mailjack@gmail.com> wrote in message <ha42ta$dq7$1@fred.mathworks.com>...
> Thanks Matt
> Any other way to do it????

Why you need other way? Matt's solution is one of a better way, so you need to elaborate why you need something else.

Here is "another way", but essentially the same thing and much more muddy code:

n=[3 1 2]

M=[1 1 0 0
1 0 0 0
0 0 1 1
0 0 1 0];

c=num2cell([n; n],2);
[c{:}] = ndgrid(c{:});
c=reshape(cat(length(c)+1,c{:}),[],length(c));
NM = reshape(M(sub2ind(size(M),c(:,1),c(:,2))),3,3)

% Bruno

Subject: matrix

From: James Tursa

Date: 2 Oct, 2009 08:00:19

Message: 6 of 11

"jack j" <12mailjack@gmail.com> wrote in message <ha2tsa$jug$1@fred.mathworks.com>...
> I have a matrix M=[1 1 0 0
> 1 0 0 0
> 0 0 1 1
> 0 0 1 0]
> If n=[3 1 2], I've to generate a matrix s=[(3,3) (3,1) (3,2)
> (1,3) (1,1) (1,2)
> (2,3) (2,1) (2,2)]
>
> Expected result should be s=[1 0 0
> 0 1 1
> 0 1 0]
> Can anyone help me?

s = M(n,n)

James Tursa

Subject: matrix

From: Bruno Luong

Date: 3 Oct, 2009 09:07:01

Message: 7 of 11

"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <ha4buj$g4v$1@fred.mathworks.com>...

> s = M(n,n)
>
> James Tursa

Doh!!!

Bruno

Subject: matrix

From: James Tursa

Date: 3 Oct, 2009 09:36:02

Message: 8 of 11

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ha747l$l1t$1@fred.mathworks.com>...
> "James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <ha4buj$g4v$1@fred.mathworks.com>...
>
> > s = M(n,n)
> >
> > James Tursa
>
> Doh!!!
>
> Bruno

But it was very interesting to read all of the other solutions! :)

James Tursa

Subject: matrix

From: Matt Fig

Date: 3 Oct, 2009 14:09:01

Message: 9 of 11

"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <ha75u2$qen$1@fred.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ha747l$l1t$1@fred.mathworks.com>...
> > "James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <ha4buj$g4v$1@fred.mathworks.com>...
> >
> > > s = M(n,n)
> > >
> > > James Tursa
> >
> > Doh!!!
> >
> > Bruno


I'll second that Doh.


> But it was very interesting to read all of the other solutions! :)
>
> James Tursa


I like your style James. Just enough sarcasm to rub it in a bit where it is deserved, but not enough to come off like a ____. Cheers!

Subject: matrix

From: James Tursa

Date: 3 Oct, 2009 20:14:02

Message: 10 of 11

"Matt Fig" <spamanon@yahoo.com> wrote in message <ha7ltt$hpo$1@fred.mathworks.com>...
>
> I like your style James. Just enough sarcasm to rub it in a bit where it is deserved, but not enough to come off like a ____. Cheers!

I've been on the receiving end of these often enough to know what it feels like! It's nice to be on the other side this time.

James Tursa

Subject: matrix

From: jack j

Date: 4 Oct, 2009 14:04:01

Message: 11 of 11

"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <ha8baa$pqi$1@fred.mathworks.com>...
> "Matt Fig" <spamanon@yahoo.com> wrote in message <ha7ltt$hpo$1@fred.mathworks.com>...
> >
> > I like your style James. Just enough sarcasm to rub it in a bit where it is deserved, but not enough to come off like a ____. Cheers!
>
> I've been on the receiving end of these often enough to know what it feels like! It's nice to be on the other side this time.
>
> James Tursa

Thatz gr8 James......

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Contact us at files@mathworks.com