Thread Subject: vector operation, selecting chunks

Subject: vector operation, selecting chunks

From: Lorenzo Guerrasio

Date: 20 Mar, 2009 16:20:17

Message: 1 of 3

Hi guys,

let say I have vector v=100x1, and a matrix M Nx2 with the beginning of Nth chunks in the first coulmn and the end of it in the second.
Is there a smart way to have a second vector v2 made of the chunks of vector v (i mean, without for loop)?

example
v=rand(100,1);
M=[1,4;7,8];

wished result

v2= v(1) v(2) v(3) v(4) v(7) v(8)

thanks in advance

Subject: vector operation, selecting chunks

From: Matt Fig

Date: 20 Mar, 2009 17:20:18

Message: 2 of 3

One approach using old-school vectorization:


% Some data:
v=round(rand(100,1)*9);
M=[1,4;7,8;80,93];

% The engine:
L = size(M,1);
R = cumsum(M(:,2)-M(:,1)+1);
IDX = ones(1,R(end));
IDX(1) = M(1,1);
IDX(1 + R(1:end-1)) = M(2:L,1) - M(1:L-1,2);
v2 = v(cumsum(IDX));




uujj$_ed^|aW0eYW[[OuukuCXj[fue_f6[k^du]WXucYWelec^;WbeoWbW|

Subject: vector operation, selecting chunks

From: Lorenzo Guerrasio

Date: 20 Mar, 2009 17:35:02

Message: 3 of 3

I've so much to learn
thanks a lot

"Matt Fig" <spamanon@yahoo.com> wrote in message <gq0j8i$if9$1@fred.mathworks.com>...
> One approach using old-school vectorization:
>
>
> % Some data:
> v=round(rand(100,1)*9);
> M=[1,4;7,8;80,93];
>
> % The engine:
> L = size(M,1);
> R = cumsum(M(:,2)-M(:,1)+1);
> IDX = ones(1,R(end));
> IDX(1) = M(1,1);
> IDX(1 + R(1:end-1)) = M(2:L,1) - M(1:L-1,2);
> v2 = v(cumsum(IDX));
>
>
>
>
> uujj$_ed^|aW0eYW[[OuukuCXj[fue_f6[k^du]WXucYWelec^;WbeoWbW|

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
vectorization Lorenzo Guerrasio 20 Mar, 2009 13:40:20
vector Lorenzo Guerrasio 20 Mar, 2009 12:25:05
inicization Lorenzo Guerrasio 20 Mar, 2009 12:25:05
rssFeed for this Thread
 

MATLAB Central Terms of Use

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 Terms prior to use.

Contact us at files@mathworks.com