MATLAB: Using vectors as the start and end of indices

indexing

I have a vector (for example):
A = randn(1,50)
from which I would like to access at specified indices. Those specific indices are listed in two vectors which indicate the start and end. For example:
start = [1, 22, 40]
finish = [10, 30, 48].
Without using a loop, can I access use vectors start and finish to access elements from A. Specifically, elements 1:10, 22:30, and 40:48?
Thanks.

Best Answer

If you want to be able to input any start and finish vectors:
>> start = [1, 22, 40];
>> finish = [10, 30, 48];
>> out = arrayfun(@(s,f)s:f,start,finish,'UniformOutput',false);
>> out{:}
ans =
1 2 3 4 5 6 7 8 9 10
ans =
22 23 24 25 26 27 28 29 30
ans =
40 41 42 43 44 45 46 47 48
>> idx = [out{:}]
idx =
1 2 3 4 5 6 7 8 9 10 22 23 24 25 26 27 28 29 30 40 41 42 43 44 45 46 47 48
and then simply:
A(idx)