Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Creating a matrix from information in two other matrixes
Date: Fri, 10 Dec 2010 18:04:06 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 46
Message-ID: <idtq2m$31p$1@fred.mathworks.com>
References: <idtaut$j28$1@fred.mathworks.com> <idtc4d$ant$1@fred.mathworks.com> <idtgfm$508$1@fred.mathworks.com> <idthuu$3c1$1@fred.mathworks.com> <idtib5$aj4$1@fred.mathworks.com> <idtlvd$j0e$1@fred.mathworks.com> <idtmnq$3j2$1@fred.mathworks.com> <idtot4$bha$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1292004246 3129 172.30.248.35 (10 Dec 2010 18:04:06 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 10 Dec 2010 18:04:06 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1861624
Xref: news.mathworks.com comp.soft-sys.matlab:694316

"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