From: Carl on
Hi,

Thanks for reading this post....

I would like to select many small vectors from large vector and store them in a matrix. The small vectors start at irregular intervals so the reshape() function may not be the best function to use.

I am currently using a for-loop to do the job but it is extremely slow. Below is an example of what I would like to see:

% here is the index noting the start of the data
index = [1 4 10 15];

% this is the long vector
A = [ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20];

% here is what I would like the Matrix to look like. I'm selecting 2 data points from % the long vector and they start at the index values

B = [1 2; 4 5; 10 11; 15 16];

I've been generating B in a for-loop but it is slow. Any tricks to help with this would be greatly appreciated.

- Carl
From: Walter Roberson on
Carl wrote:
> Hi,
>
> Thanks for reading this post....
>
> I would like to select many small vectors from large vector and store
> them in a matrix. The small vectors start at irregular intervals so the
> reshape() function may not be the best function to use.
> I am currently using a for-loop to do the job but it is extremely
> slow. Below is an example of what I would like to see:
>
> % here is the index noting the start of the data
> index = [1 4 10 15];
>
> % this is the long vector
> A = [ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20];
>
> % here is what I would like the Matrix to look like. I'm selecting 2
> data points from % the long vector and they start at the index values
>
> B = [1 2; 4 5; 10 11; 15 16];
>
> I've been generating B in a for-loop but it is slow. Any tricks to help
> with this would be greatly appreciated.

A([index(:), index(:) + 1])