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:
extract elements from a vector using index

Subject: extract elements from a vector using index

From: Patrick Diviacco

Date: 16 Sep, 2009 14:04:02

Message: 1 of 2

Hi,

if I have a index A= [1 0 1 0 0 0 1] and a vector B = [2 3 4 1 2 5 3 ], how do I extract the elements from B corresponding to "1" in A ?

thanks

Subject: extract elements from a vector using index

From: Steven Lord

Date: 16 Sep, 2009 14:12:47

Message: 2 of 2


"Patrick Diviacco" <patrick.diviacco@gmail.com> wrote in message
news:h8qr8i$sq9$1@fred.mathworks.com...
> Hi,
>
> if I have a index A= [1 0 1 0 0 0 1] and a vector B = [2 3 4 1 2 5 3 ],
> how do I extract the elements from B corresponding to "1" in A ?

Use logical indexing.

C = B(A==1)

If by "extract" you instead mean you need a matrix the same size as B but
with 0's in the locations where A is 0, use:

C = B;
C(A==0) = 0
% or if all the elements of B are finite:
C = B.*A

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Tags for this Thread

No tags are associated with 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