Grabbing sections of a matrix by using two doubles as the index

1 回表示 (過去 30 日間)
Ben Anstrom
Ben Anstrom 2019 年 12 月 23 日
コメント済み: Ben Anstrom 2019 年 12 月 24 日
Hello everyone,
I am trying to access sections of an array such as:
data = [10 10.5 11.5 13 15 16 20.5 24];
by using the following two arrays:
indStart = [1 0 0 0 1 0 0 0]; indEnd = [0 0 1 0 0 0 0 1];
The final output should look something like this:
result = { [10 1.5 11.5] }
{ [15 16 20.5 24] }
This is what i have tried, along with little variations of it:
result{ : } = data(indStart : indEnd);
The results of my method is either an error, or i can access 1:3, but not get 5:8.
I know i can do this quite easily with a for loop, but i am trying to accomplish this using indexing and logical statements so that processing time stays down. The final script needs to be processed by ga(), so any cut down on processing time is very valuable.
Thank you to anyone who can help!

採用された回答

James Tursa
James Tursa 2019 年 12 月 23 日
編集済み: James Tursa 2019 年 12 月 23 日
E.g.,
result = arrayfun(@(x1,x2)data(x1:x2),find(indStart),find(indEnd),'uni',false);
This assumes of course that the indStart and indEnd are consistent and have the appropriate number of 1's etc.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by