Got Questions? Get Answers.
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:
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

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