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?
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
"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 ...
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.