Thread Subject: Matrix : B (A)

Subject: Matrix : B (A)

From: susan

Date: 29 Jan, 2008 20:44:02

Message: 1 of 3

Hi! If I have a matrix a= [ 10 11 12 13; 14 15 16 17];
and I want 2nd and 3rd element from each row so b=[2 3; 2
3];
is there a way to do a(b) to get [11 12; 15 16]; without
having to do it row by row?

Thanks,
S

Subject: Matrix : B (A)

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 29 Jan, 2008 20:59:57

Message: 2 of 3

In article <fno36i$pid$1@fred.mathworks.com>,
susan <suetibu@hotmail.com> wrote:
>Hi! If I have a matrix a= [ 10 11 12 13; 14 15 16 17];
>and I want 2nd and 3rd element from each row so b=[2 3; 2
>3];
>is there a way to do a(b) to get [11 12; 15 16]; without
>having to do it row by row?

a(:,[2 3])

If you need the indices to be derived from another variable
and that variable can be expressed as a single row, such as
b = [2 3]; then you can use a(:,b)

If you need the indices to be derived from another variable
and the variable cannot be expressed as a single row (perhaps
because you are extracting different elements from the different
rows) then you have a more complex task that will likely involve
sub2ind() at some point.
--
   "Beware of bugs in the above code; I have only proved it correct,
   not tried it." -- Donald Knuth

Subject: Matrix : B (A)

From: Jos

Date: 29 Jan, 2008 21:03:01

Message: 3 of 3

"susan " <suetibu@hotmail.com> wrote in message
<fno36i$pid$1@fred.mathworks.com>...
> Hi! If I have a matrix a= [ 10 11 12 13; 14 15 16 17];
> and I want 2nd and 3rd element from each row so b=[2 3; 2
> 3];
> is there a way to do a(b) to get [11 12; 15 16]; without
> having to do it row by row?
>
> Thanks,
> S


a= [ 10 11 12 13; 14 15 16 17];
b = 2:3 ;
a(:,b)

May I suggest to you to read the "Getting Started" section
of the (online) manual ...

hth
Jos

Tags for this Thread

Everyone's Tags:

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.

Tag Activity for This Thread
Tag Applied By Date/Time
matrix susan 29 Jan, 2008 15:44:55
rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com