フィルターのクリア

Hello, I have an array of values in "In", I would like to extract the samples in "In" between the indices specified in "SIdx" and "EIdx". SIdx and EIdx are also arrays.

1 回表示 (過去 30 日間)
I would like to acheive the functionality without using a for loop. Currently the code is written as
Working Code :
In = 1:100;
SIdx = [1 9 33 76];
EIdx = [5 13 42 83];
Out = {};
for i = 1:length(SIdx)
Out = [Out; {In(SIdx(i):EIdx(i))}];
end
Out =
4×1 cell array
{[ 1 2 3 4 5]}
{[ 9 10 11 12 13]}
{[33 34 35 36 37 38 39 40 41 42]}
{[ 76 77 78 79 80 81 82 83]}
Is there a way to acheive the same functionality without using a for loop.

採用された回答

Voss
Voss 2024 年 3 月 9 日
編集済み: Voss 2024 年 3 月 9 日
In = 1:100;
SIdx = [1 9 33 76];
EIdx = [5 13 42 83];
siz = zeros(1,2*numel(SIdx)-1);
siz(1:2:end) = EIdx-SIdx+1;
siz(2:2:end) = SIdx(2:end)-EIdx(1:end-1)-1;
Out = mat2cell(In(SIdx(1):EIdx(end)),1,siz).';
Out(2:2:end) = [];
disp(Out)
{[ 1 2 3 4 5]} {[ 9 10 11 12 13]} {[33 34 35 36 37 38 39 40 41 42]} {[ 76 77 78 79 80 81 82 83]}

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2024 年 3 月 9 日
In = 1:100;
SIdx = [1 9 33 76];
EIdx = [5 13 42 83];
Out = arrayfun(@(S,E) In(S:E), SIdx, EIdx, 'uniform', 0).'
Out = 4×1 cell array
{[ 1 2 3 4 5]} {[ 9 10 11 12 13]} {[33 34 35 36 37 38 39 40 41 42]} {[ 76 77 78 79 80 81 82 83]}
  2 件のコメント
Krishna Ghanakota
Krishna Ghanakota 2024 年 3 月 9 日
arrayfun(func,A) applies the function func to the elements of A, one element at a time.
The size of "SIdx" can grow very large and I would like to avoid looping.
Is there a way in which "In" can be vector indexed with SIdx and EIdx.
Krishna Ghanakota
Krishna Ghanakota 2024 年 3 月 9 日
The In, SIdx, EIdx values given in the example code are just for illustrating the idea. These are in actual very large arrays and looping takes quite some time.

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

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by