フィルターのクリア

splitting an array of numbers

2 ビュー (過去 30 日間)
Greg
Greg 2012 年 10 月 10 日
I have an array of numbers, for example numbers = rand(1000,1);
I have another array of numbers indicating the offsets for the groups, for example,
offsets = [1, 100, 250, 900, 1000];
so the
first group = numbers(1:100)
second group = numbers(101:250)
third group = numbers(251:900)
fourth group = numbers(901:1000)
I could use a for loop to identify the groups but I want to know if there is a faster way.

回答 (2 件)

Andrei Bobrov
Andrei Bobrov 2012 年 10 月 10 日
numbers = rand(1000,1);
offsets = [0, 100, 250, 900, 1000];
out = mat2cell(numbers,diff(offsets),1);

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 10 月 10 日
編集済み: Azzi Abdelmalek 2012 年 10 月 10 日
v=rand(1000,1)
offsets = [0, 100, 250, 900, 1000];
out=arrayfun(@(x) v(offsets(x)+1:offsets(x+1)),1:numel(offsets)-1,'uni',false)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by