Get several subsets from a set using two arrays.

Hello.
For example.
I have the following set:
exSet = 21:40;
And 2 arrays, one with the indexes of the lower position and the second with the indexes of the higher position.
Lower position array:
lPos = [2 8];
Upper position array:
uPos = [11 16];
The resulting subsets are:
subSet1 = extSet(lPos(1) : uPos(1)) = 22:31;
subSet2 = extSet(lPos(2) : uPos(2)) = 28:36;
But, I want to do this in a single step because both my set exSet and the arrays have much more elements.
To avoid a for loop, I will appreciate suggestions.
Best Regards,

2 commentaires

What do you mean by "in a single step"?
Well, it is more like an efficient path to solve the problem instead of using a loop.
Of course, if there isn't a solution to solve the problem in a single step, or in just one code line, i will be thankful if you give me the best solution you know.

Connectez-vous pour commenter.

 Réponse acceptée

Massimo Zanetti
Massimo Zanetti le 28 Sep 2016
Modifié(e) : Massimo Zanetti le 28 Sep 2016
Ok, I got it. You can work like this, by exploiting arrayfun, which iterates an operation on array elements.
vect = 101:120;
s(1).lim=[11,15];
s(2).lim=[4,9];
s(3).lim=[9,18];
ext = arrayfun( @(x) vect(x.lim(1):x.lim(2)) , s , 'UniformOutput' , false);
ext{1}
ext{2}
ext{3}
As you can see, in just one call, it is possible to extact from vect as many sub-vectors as we want. :)

Catégories

En savoir plus sur Data Type Identification dans Centre d'aide et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by