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:
Creating a matrix from information in two other matrixes

Subject: Creating a matrix from information in two other matrixes

From: Ulrik Nash

Date: 10 Dec, 2010 13:46:05

Message: 1 of 18

Hi Everyone,

Suppose I have a matrix A

A = [2 3 1;1 5 0]

And another matrix B

B = [2 1 3;2 1 3]

Now I wish to create a matrix C, which looks like this

C = [3 2 1;5 1 0]

Notice that:

The first line, first column of C contains that value in line one of A, corresponding to the 1 found in the first line of B.

The first line, second column of C contains that value in line one of A, corresponding to the 2 found in the first line of B.

The first line, third column of C contains that value in line one of A, corresponding to the 3 found in the first line of B.

and so forth for other lines....

What code will create C from A and B?

Kind regards,

Ulrik.

Subject: Creating a matrix from information in two other matrixes

From: Grzegorz Knor

Date: 10 Dec, 2010 14:06:05

Message: 2 of 18

for example:

A = [A(1,B(1,:));A(2,B(2,:))]

Grzegorz

Subject: Creating a matrix from information in two other matrixes

From: Ulrik Nash

Date: 10 Dec, 2010 15:20:23

Message: 3 of 18

"Grzegorz Knor" <gknor@o2.pl> wrote in message <idtc4d$ant$1@fred.mathworks.com>...
> for example:
>
> A = [A(1,B(1,:));A(2,B(2,:))]
>
> Grzegorz

Thanks Grzegorz

But what if there are n rows. This appears to work only on two rows (I know I provided an example with two rows, but I need to generalize).

Regards,

Ulrik.

Subject: Creating a matrix from information in two other matrixes

From: Oleg Komarov

Date: 10 Dec, 2010 15:41:05

Message: 4 of 18

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <idtgfm$508$1@fred.mathworks.com>...
> "Grzegorz Knor" <gknor@o2.pl> wrote in message <idtc4d$ant$1@fred.mathworks.com>...
> > for example:
> >
> > A = [A(1,B(1,:));A(2,B(2,:))]
> >
> > Grzegorz
>
> Thanks Grzegorz
>
> But what if there are n rows. This appears to work only on two rows (I know I provided an example with two rows, but I need to generalize).
>
> Regards,
>
> Ulrik.

szA = size(A);
idx = sub2ind(szA, repmat(1:szA(1),szA(2),1).', B);

A(idx)

Subject: Creating a matrix from information in two other matrixes

From: Sean de

Date: 10 Dec, 2010 15:45:34

Message: 5 of 18

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <idtgfm$508$1@fred.mathworks.com>...
> "Grzegorz Knor" <gknor@o2.pl> wrote in message <idtc4d$ant$1@fred.mathworks.com>...
> > for example:
> >
> > A = [A(1,B(1,:));A(2,B(2,:))]
> >
> > Grzegorz
>
> Thanks Grzegorz
>
> But what if there are n rows. This appears to work only on two rows (I know I provided an example with two rows, but I need to generalize).
>
> Regards,
>
> Ulrik.

perhaps

C = bsxfun(@(x,y)x(:,y(1:size(x,1):end)),A,B);

Subject: Creating a matrix from information in two other matrixes

From: Sean de

Date: 10 Dec, 2010 15:52:05

Message: 6 of 18


> C = bsxfun(@(x,y)x(:,y(1:size(x,1):end)),A,B);

Don't even need bsxfun, they're the same size!

A(:,B(1:size(A,1):end))

Subject: Creating a matrix from information in two other matrixes

From: Ulrik Nash

Date: 10 Dec, 2010 16:51:22

Message: 7 of 18

"Oleg Komarov" <oleg.komarovRemove.this@hotmail.it> wrote in message <idthmg$rso$1@fred.mathworks.com>...
> "Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <idtgfm$508$1@fred.mathworks.com>...
> > "Grzegorz Knor" <gknor@o2.pl> wrote in message <idtc4d$ant$1@fred.mathworks.com>...
> > > for example:
> > >
> > > A = [A(1,B(1,:));A(2,B(2,:))]
> > >
> > > Grzegorz
> >
> > Thanks Grzegorz
> >
> > But what if there are n rows. This appears to work only on two rows (I know I provided an example with two rows, but I need to generalize).
> >
> > Regards,
> >
> > Ulrik.
>
> szA = size(A);
> idx = sub2ind(szA, repmat(1:szA(1),szA(2),1).', B);
>
> A(idx)

How unfortunate!! This solution is not what I am after, but I gave an example, where there are two approaches which give the same answer. The approach I am after is not the one you provided Grzegorz.

Lets have a look:

A = [2 3 1;1 5 0]
A =

     2 3 1
     1 5 0

B = [2 1 3;2 1 3]
B =

     2 1 3
     2 1 3

Grzegorz Answer (correct here, but is not in general what i am after):
C =
     3 2 1
     5 1 0

The proceedure I need (in words) is as follows:
C(1,1):
"Look at B's row 1. Locate the number 1. Find the corresponding number i A. Answer=3"

Grzegorz proceedure (in words) is as follows:
C(1,1):
"Look at B(1,1). The number is 2. Find A(1,2).
Answer=3"

The proceedures give the same final answer for the example given, but not generally. I need the first proceedure...

So, my question is, how?!

Subject: Creating a matrix from information in two other matrixes

From: Oleg Komarov

Date: 10 Dec, 2010 16:54:05

Message: 8 of 18

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <idtib5$aj4$1@fred.mathworks.com>...
>
> > C = bsxfun(@(x,y)x(:,y(1:size(x,1):end)),A,B);
>
> Don't even need bsxfun, they're the same size!
>
> A(:,B(1:size(A,1):end))

It works iff B's row don't change.

Oleg

Subject: Creating a matrix from information in two other matrixes

From: Ulrik Nash

Date: 10 Dec, 2010 17:07:06

Message: 9 of 18

"Oleg Komarov" <oleg.komarovRemove.this@hotmail.it> wrote in message <idtlvd$j0e$1@fred.mathworks.com>...
> "Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <idtib5$aj4$1@fred.mathworks.com>...
> >
> > > C = bsxfun(@(x,y)x(:,y(1:size(x,1):end)),A,B);
> >
> > Don't even need bsxfun, they're the same size!
> >
> > A(:,B(1:size(A,1):end))
>
> It works iff B's row don't change.
>
> Oleg

Hi Oleg, please see my post right above. I am after a slightly different proceedure...

Ragards,

Ulrik.

Subject: Creating a matrix from information in two other matrixes

From: Ulrik Nash

Date: 10 Dec, 2010 17:44:04

Message: 10 of 18

Hi Everyone,

I will try to explain myself in more detail, because I think I might have confused many people. Instead of starting a new post, I trust that people will look here, even though things are probably more obsure down this far in the thread..... Anyway:

Suppose I have a matrix A

A = [2 3 1;1 5 0]

And another matrix B

B = [2 1 3;2 3 1]

Now I wish to create a matrix C, which looks like this

C = [3 2 1;0 1 5]

The procedure that creates C is in words as follows:
C(1,1):
"Look at B's row 1. Locate the number 1. Find the corresponding number i A. Answer=3"
C(1,2):
"Look at B's row 1. Locate the number 2. Find the corresponding number i A. Answer=2"
C(1,3):
"Look at B's row 1. Locate the number 3. Find the corresponding number i A. Answer=1"
C(2,1):
"Look at B's row 2. Locate the number 1. Find the corresponding number i A. Answer=0"
C(2,2):
"Look at B's row 2. Locate the number 2. Find the corresponding number i A. Answer=1"
C(2,3):
"Look at B's row 2. Locate the number 3. Find the corresponding number i A. Answer=5"

What code will generate C in general. In other words, the example shows rows of 2 and columns of 3. But what is there are r rows and c columns (in both A and B)?

Kind regards,

Ulrik.

Subject: Creating a matrix from information in two other matrixes

From: Grzegorz Knor

Date: 10 Dec, 2010 18:04:06

Message: 11 of 18

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <idtot4$bha$1@fred.mathworks.com>...
> Hi Everyone,
>
> I will try to explain myself in more detail, because I think I might have confused many people. Instead of starting a new post, I trust that people will look here, even though things are probably more obsure down this far in the thread..... Anyway:
>
> Suppose I have a matrix A
>
> A = [2 3 1;1 5 0]
>
> And another matrix B
>
> B = [2 1 3;2 3 1]
>
> Now I wish to create a matrix C, which looks like this
>
> C = [3 2 1;0 1 5]
>
> The procedure that creates C is in words as follows:
> C(1,1):
> "Look at B's row 1. Locate the number 1. Find the corresponding number i A. Answer=3"
> C(1,2):
> "Look at B's row 1. Locate the number 2. Find the corresponding number i A. Answer=2"
> C(1,3):
> "Look at B's row 1. Locate the number 3. Find the corresponding number i A. Answer=1"
> C(2,1):
> "Look at B's row 2. Locate the number 1. Find the corresponding number i A. Answer=0"
> C(2,2):
> "Look at B's row 2. Locate the number 2. Find the corresponding number i A. Answer=1"
> C(2,3):
> "Look at B's row 2. Locate the number 3. Find the corresponding number i A. Answer=5"
>
> What code will generate C in general. In other words, the example shows rows of 2 and columns of 3. But what is there are r rows and c columns (in both A and B)?
>
> Kind regards,
>
> Ulrik.

This is no elegant solution, but you can use loop:

for k=1:size(A,1)
    A(k,:) = A(k,B(k,:));
end

But I belive that it could be done without loop :)

Grzegorz

Subject: Creating a matrix from information in two other matrixes

From: Sean de

Date: 10 Dec, 2010 18:08:04

Message: 12 of 18

> Suppose I have a matrix A
>
> A = [2 3 1;1 5 0]
>
> And another matrix B
>
> B = [2 1 3;2 3 1]
>
> Now I wish to create a matrix C, which looks like this
>
> C = [3 2 1;0 1 5]
>
> The procedure that creates C is in words as follows:
> C(1,1):
> "Look at B's row 1. Locate the number 1. Find the corresponding number i A. Answer=3"
> C(1,2):
> "Look at B's row 1. Locate the number 2. Find the corresponding number i A. Answer=2"
> C(1,3):
> "Look at B's row 1. Locate the number 3. Find the corresponding number i A. Answer=1"
> C(2,1):
> "Look at B's row 2. Locate the number 1. Find the corresponding number i A. Answer=0"
> C(2,2):
> "Look at B's row 2. Locate the number 2. Find the corresponding number i A. Answer=1"
> C(2,3):
> "Look at B's row 2. Locate the number 3. Find the corresponding number i A. Answer=5"
>

Your example is wrong by your definition, it should be:
C = [3 2 1;5 0 1] since A(2,2 {2 extracted from location of b}) = 5.

Anyway:
C = bsxfun(@(x,y)A(sub2ind(size(A),x,y)),[1:size(A,1)].',B)

Subject: Creating a matrix from information in two other matrixes

From: Grzegorz Knor

Date: 10 Dec, 2010 18:18:05

Message: 13 of 18

Another solution:
tmp = repmat((1:size(A,1))',1,size(A,2))
idx = sub2ind(size(A),tmp(:),B(:))
C = reshape(A(idx),size(A))

Grzegorz

Subject: Creating a matrix from information in two other matrixes

From: Ulrik Nash

Date: 10 Dec, 2010 18:24:06

Message: 14 of 18

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <idtqa4$73v$1@fred.mathworks.com>...
> > Suppose I have a matrix A
> >
> > A = [2 3 1;1 5 0]
> >
> > And another matrix B
> >
> > B = [2 1 3;2 3 1]
> >
> > Now I wish to create a matrix C, which looks like this
> >
> > C = [3 2 1;0 1 5]
> >
> > The procedure that creates C is in words as follows:
> > C(1,1):
> > "Look at B's row 1. Locate the number 1. Find the corresponding number i A. Answer=3"
> > C(1,2):
> > "Look at B's row 1. Locate the number 2. Find the corresponding number i A. Answer=2"
> > C(1,3):
> > "Look at B's row 1. Locate the number 3. Find the corresponding number i A. Answer=1"
> > C(2,1):
> > "Look at B's row 2. Locate the number 1. Find the corresponding number i A. Answer=0"
> > C(2,2):
> > "Look at B's row 2. Locate the number 2. Find the corresponding number i A. Answer=1"
> > C(2,3):
> > "Look at B's row 2. Locate the number 3. Find the corresponding number i A. Answer=5"
> >
>
> Your example is wrong by your definition, it should be:
> C = [3 2 1;5 0 1] since A(2,2 {2 extracted from location of b}) = 5.
>
> Anyway:
> C = bsxfun(@(x,y)A(sub2ind(size(A),x,y)),[1:size(A,1)].',B)

"Your example is wrong by your definition"

I don't think it is... When locating 1 (the number one) in row two of B, it is found at location B(2,3). Now heading back to A, one finds that A(2,3) is 0.

This is turning out more difficult than it normally is! But I really appreciate the help!

Subject: Creating a matrix from information in two other matrixes

From: Sean de

Date: 10 Dec, 2010 18:30:22

Message: 15 of 18

"Grzegorz Knor" <gknor@o2.pl> wrote in message <idtqst$hsp$1@fred.mathworks.com>...
> Another solution:
> tmp = repmat((1:size(A,1))',1,size(A,2))
> idx = sub2ind(size(A),tmp(:),B(:))
> C = reshape(A(idx),size(A))
>
> Grzegorz

No need for reshape or (:)
C = A(sub2ind(size(A),tmp,B))

Subject: Creating a matrix from information in two other matrixes

From: Oleg Komarov

Date: 10 Dec, 2010 18:38:05

Message: 16 of 18

What about my post #4?

Oleg

Subject: Creating a matrix from information in two other matrixes

From: Sean de

Date: 10 Dec, 2010 18:39:05

Message: 17 of 18

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <idtr86$oeh$1@fred.mathworks.com>...
> "Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <idtqa4$73v$1@fred.mathworks.com>...
> > > Suppose I have a matrix A
> > >
> > > A = [2 3 1;1 5 0]
> > >
> > > And another matrix B
> > >
> > > B = [2 1 3;2 3 1]
> > >
> > > Now I wish to create a matrix C, which looks like this
> > >
> > > C = [3 2 1;0 1 5]
> > >
> > > The procedure that creates C is in words as follows:
> > > C(1,1):
> > > "Look at B's row 1. Locate the number 1. Find the corresponding number i A. Answer=3"
> > > C(1,2):
> > > "Look at B's row 1. Locate the number 2. Find the corresponding number i A. Answer=2"
> > > C(1,3):
> > > "Look at B's row 1. Locate the number 3. Find the corresponding number i A. Answer=1"
> > > C(2,1):
> > > "Look at B's row 2. Locate the number 1. Find the corresponding number i A. Answer=0"
> > > C(2,2):
> > > "Look at B's row 2. Locate the number 2. Find the corresponding number i A. Answer=1"
> > > C(2,3):
> > > "Look at B's row 2. Locate the number 3. Find the corresponding number i A. Answer=5"
> > >
> >
> > Your example is wrong by your definition, it should be:
> > C = [3 2 1;5 0 1] since A(2,2 {2 extracted from location of b}) = 5.
> >
> > Anyway:
> > C = bsxfun(@(x,y)A(sub2ind(size(A),x,y)),[1:size(A,1)].',B)
>
> "Your example is wrong by your definition"
>
> I don't think it is... When locating 1 (the number one) in row two of B, it is found at location B(2,3). Now heading back to A, one finds that A(2,3) is 0.
>
> This is turning out more difficult than it normally is! But I really appreciate the help!


Ahhhh, so you're looking for the equivalent index in B, not using it!

[junk, Bidx] = sort(B,2);
C = bsxfun(@(x,y)A(sub2ind(size(A),x,y)),[1:size(A,1)].',Bidx)

Subject: Creating a matrix from information in two other matrixes

From: Ulrik Nash

Date: 10 Dec, 2010 18:52:04

Message: 18 of 18

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <idts49$ab2$1@fred.mathworks.com>...
> "Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <idtr86$oeh$1@fred.mathworks.com>...
> > "Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <idtqa4$73v$1@fred.mathworks.com>...
> > > > Suppose I have a matrix A
> > > >
> > > > A = [2 3 1;1 5 0]
> > > >
> > > > And another matrix B
> > > >
> > > > B = [2 1 3;2 3 1]
> > > >
> > > > Now I wish to create a matrix C, which looks like this
> > > >
> > > > C = [3 2 1;0 1 5]
> > > >
> > > > The procedure that creates C is in words as follows:
> > > > C(1,1):
> > > > "Look at B's row 1. Locate the number 1. Find the corresponding number i A. Answer=3"
> > > > C(1,2):
> > > > "Look at B's row 1. Locate the number 2. Find the corresponding number i A. Answer=2"
> > > > C(1,3):
> > > > "Look at B's row 1. Locate the number 3. Find the corresponding number i A. Answer=1"
> > > > C(2,1):
> > > > "Look at B's row 2. Locate the number 1. Find the corresponding number i A. Answer=0"
> > > > C(2,2):
> > > > "Look at B's row 2. Locate the number 2. Find the corresponding number i A. Answer=1"
> > > > C(2,3):
> > > > "Look at B's row 2. Locate the number 3. Find the corresponding number i A. Answer=5"
> > > >
> > >
> > > Your example is wrong by your definition, it should be:
> > > C = [3 2 1;5 0 1] since A(2,2 {2 extracted from location of b}) = 5.
> > >
> > > Anyway:
> > > C = bsxfun(@(x,y)A(sub2ind(size(A),x,y)),[1:size(A,1)].',B)
> >
> > "Your example is wrong by your definition"
> >
> > I don't think it is... When locating 1 (the number one) in row two of B, it is found at location B(2,3). Now heading back to A, one finds that A(2,3) is 0.
> >
> > This is turning out more difficult than it normally is! But I really appreciate the help!
>
>
> Ahhhh, so you're looking for the equivalent index in B, not using it!
>
> [junk, Bidx] = sort(B,2);
> C = bsxfun(@(x,y)A(sub2ind(size(A),x,y)),[1:size(A,1)].',Bidx)


BINGO! Thank you very much guys, and have a great weekend!

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