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 件のコメント

Massimo Zanetti
Massimo Zanetti 2016 年 9 月 28 日
What do you mean by "in a single step"?
Tiago Marçal
Tiago Marçal 2016 年 9 月 28 日
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.

サインインしてコメントする。

 採用された回答

Massimo Zanetti
Massimo Zanetti 2016 年 9 月 28 日
編集済み: Massimo Zanetti 2016 年 9 月 28 日

3 投票

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. :)

その他の回答 (1 件)

Tiago Marçal
Tiago Marçal 2016 年 9 月 29 日

0 投票

It works.
Thanks.

カテゴリ

ヘルプ センター および File ExchangeOperators and Elementary Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by