Thread Subject: matrix manipulation

Subject: matrix manipulation

From: Ross

Date: 28 Nov, 2008 21:02:47

Message: 1 of 5

Hi there,

I have a question regarding matrix manipulation. I am using a 'brute force' method, but I guess there might be a straightforward way to do this that I am not aware of.

Assume you have array A, s.t. [N1 N2 N3] = size(A).
Also, assume you have a vector p, s.t. N1 = length(p), whose entries are integers between 1 and N3.

You want to obtain a new matrix B, N1-by-N2 where the rows are selected using p.

Example:
A = [1 2;3 4];
A(:,:,2) = [10 20;30 40]
p = [1 2];

B = [1 2; 30 40];


Thanks!
Ross

Subject: matrix manipulation

From: Bruno Luong

Date: 28 Nov, 2008 21:30:20

Message: 2 of 5

Ross <fed.rossi@gmail.com> wrote in message <26885583.1227906197694.JavaMail.jakarta@nitrogen.mathforum.org>...
> Hi there,
>
> I have a question regarding matrix manipulation. I am using a 'brute force' method, but I guess there might be a straightforward way to do this that I am not aware of.
>
> Assume you have array A, s.t. [N1 N2 N3] = size(A).
> Also, assume you have a vector p, s.t. N1 = length(p), whose entries are integers between 1 and N3.
>
> You want to obtain a new matrix B, N1-by-N2 where the rows are selected using p.
>
> Example:
> A = [1 2;3 4];
> A(:,:,2) = [10 20;30 40]
> p = [1 2];
>
> B = [1 2; 30 40];
>
>

% Data
A = [1 2;
     3 4;
     5 6];
A(:,:,2) = [10 20;
            30 40;
            50 60]

[N1 N2 N3]=size(A);

p = [1 2 2];

% Engine 1
[I J]=ndgrid(1:N1,1:N2);
K=repmat(p(:),1,N2);
B = A(sub2ind(size(A),I,J,K))

% Engine 2
Ap=permute(A,[2 1 3]);
B=Ap(:,sub2ind([N1 N3],1:N1,p)).'

% Bruno

Subject: matrix manipulation

From: Walter Roberson

Date: 28 Nov, 2008 21:42:11

Message: 3 of 5

Ross wrote:
> Assume you have array A, s.t. [N1 N2 N3] = size(A).
> Also, assume you have a vector p, s.t. N1 = length(p), whose entries are integers between 1 and N3.
>
> You want to obtain a new matrix B, N1-by-N2 where the rows are selected using p.
>
> Example:
> A = [1 2;3 4];
> A(:,:,2) = [10 20;30 40]
> p = [1 2];
>
> B = [1 2; 30 40];
>

That's pretty much the same task as was explored by a previous poster; you should be
able to adapt the solution I gave in

http://groups.google.ca/group/comp.soft-sys.matlab/browse_thread/thread/2316f0c1c23f97b4

--
.signature note: I am now avoiding replying to unclear or ambiguous postings.
Please review questions before posting them. Be specific. Use examples of what you mean,
of what you don't mean. Specify boundary conditions, and data classes and value
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?

Subject: matrix manipulation

From: Ross

Date: 28 Nov, 2008 22:22:34

Message: 4 of 5

Thanks!

Subject: matrix manipulation

From: Ross

Date: 28 Nov, 2008 22:28:59

Message: 5 of 5

ok, thanks

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