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:
Matrix index

Subject: Matrix index

From: Frank

Date: 30 Jun, 2010 10:30:10

Message: 1 of 4

Hi,

If I have a matrix A with dimension 5 x 5 and I want create a submatrix:

B = [A(1,2), A(2,3); A(4,3), A(5,1)];

How can I create this submatrix with the corresponding x-indeces and y-indeces, xc and yc?

xc = [1,2;4,5];
yc = [2,3;3,1];

I tried A(xc, yc) but it failed to produce B.

What is the correct approach?

Thanks a lot

Subject: Matrix index

From: us

Date: 30 Jun, 2010 10:41:04

Message: 2 of 4

"Frank " <allinone_2003@yahoo.com.hk> wrote in message <i0f6bi$s72$1@fred.mathworks.com>...
> Hi,
>
> If I have a matrix A with dimension 5 x 5 and I want create a submatrix:
>
> B = [A(1,2), A(2,3); A(4,3), A(5,1)];
>
> How can I create this submatrix with the corresponding x-indeces and y-indeces, xc and yc?
>
> xc = [1,2;4,5];
> yc = [2,3;3,1];
>
> I tried A(xc, yc) but it failed to produce B.
>
> What is the correct approach?
>
> Thanks a lot

one of the solutions

     xc=[1,2;4,5];
     yc=[2,3;3,1];
     m=magic(5);
     r=arrayfun(@(x,y) m(x,y),xc,yc)
%{
% r =
     24 7
     19 11
%}

us

Subject: Matrix index

From: Frank

Date: 30 Jun, 2010 17:14:05

Message: 3 of 4

Thank you, us.

Subject: Matrix index

From: Matt Fig

Date: 30 Jun, 2010 17:27:07

Message: 4 of 4

Also, following the example given by us,

r = m(sub2ind(size(m),xc,yc))

which is a shortcut to:

r = m(xc+(yc-1)*size(m,1))

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