Separate arrays within loop using indexing?

2 ビュー (過去 30 日間)
Nicholas Kavouris
Nicholas Kavouris 2022 年 4 月 5 日
回答済み: Walter Roberson 2022 年 4 月 5 日
I have matricies for start and stop time of system cycle. How can i iterate this over a raw dataset so that each loop produces a new matirix containing the points from start(i) to stop(i)?
start and stop will have equal elements but may have numel 1-20
if start has 5 elements loop will produce data.1-data.5
What im looking for:
ex start=[1,8]; stop=[7,11]
data=[2,2,2,3,3,4,4,2,3,4,4,2]
ans=
data.1=[2,2,2,3,3,4,4]
data.2=[2,3,4,4]
have tried:
for k=1:numel(start)
seperated_data(k)=data(start(k):stop(k))
end

採用された回答

DGM
DGM 2022 年 4 月 5 日
You should just be able to use a cell array.
start = [1,8];
stop = [7,11];
data = [2,2,2,3,3,4,4,2,3,4,4,2];
% same thing, but with a cell array
seperated_data = cell(numel(start),1);
for k = 1:numel(start)
seperated_data{k}=data(start(k):stop(k));
end
% show the contents of the output
celldisp(seperated_data)
seperated_data{1} = 2 2 2 3 3 4 4 seperated_data{2} = 2 3 4 4
  1 件のコメント
Nicholas Kavouris
Nicholas Kavouris 2022 年 4 月 5 日
Worked like a charm! Thank you.

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2022 年 4 月 5 日
start = [1,8];
stop = [7,11];
data = [2,2,2,3,3,4,4,2,3,4,4,2];
seperated_data = arrayfun(@(B,E) data(B:E), start, stop, 'uniform', 0)
seperated_data = 1×2 cell array
{[2 2 2 3 3 4 4]} {[2 3 4 4]}

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by