How to perform the following data splitting?

3 ビュー (過去 30 日間)
M
M 2023 年 10 月 1 日
コメント済み: M 2023 年 10 月 2 日
How to perform the following data splitting?
There are vectors X,Y,T all of these have the same size
I want to split these 3 vectors and store the splitted data in 3 arraies X_array y_array T_array
The splitting will be according to a specific range
for example:
split X and Y according to T
T contains time data from 5.2 to 10 (i,e T=[5.2,5.2002,.........................5.202, ................10])
I want to split these vectors every 0.002 of T
for example include all the data of X in one vector until T reach 5.202 then store in the first cell in the array,then include all the data of X in one vector until T reach 5.204 then store in the 2nd cell in the array,,, and so on
similar to Y and T

採用された回答

Dyuman Joshi
Dyuman Joshi 2023 年 10 月 1 日
%Random data
t = 5.2:0.002:5.3;
x = 1:numel(t);
%Define bins to group data into
cats = min(t):0.02:max(t);
%Group the data according to the bins defined
idx = discretize(t,cats);
%Split the data according to groups and store in a cell array
%Do the same for y as well
X = splitapply(@(k) {k}, x, idx)
X = 1×5 cell array
{[1 2 3 4 5 6 7 8 9 10]} {[11 12 13 14 15 16 17 18 19 20]} {[21 22 23 24 25 26 27 28 29 30]} {[31 32 33 34 35 36 37 38 39 40]} {1×11 double}
X'
ans = 5×1 cell array
{[ 1 2 3 4 5 6 7 8 9 10]} {[ 11 12 13 14 15 16 17 18 19 20]} {[ 21 22 23 24 25 26 27 28 29 30]} {[ 31 32 33 34 35 36 37 38 39 40]} {[41 42 43 44 45 46 47 48 49 50 51]}
  2 件のコメント
Walter Roberson
Walter Roberson 2023 年 10 月 1 日
We probably should not count on the input T starting from 5.2 so we should probably not use min() for the lower bound. We are told to start from 5.2 so that should be the lower bound.
M
M 2023 年 10 月 2 日
@Dyuman Joshi, Thanks, your solution worked with me and the splitting was correct

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2023 年 10 月 1 日
idx = round((T(:)-5.2)/0.02) + 1;
Xgrouped = accumarray(idx, X(:), [], @(vals){vals});
Ygrouped = accumarray(idx, Y(:), [], @(vals){vals});
Tgrouped = accumarray(idx, T(:), [], @(vals){vals});
  3 件のコメント
M
M 2023 年 10 月 2 日
編集済み: M 2023 年 10 月 2 日
idx = round((Time(:)-Time(1,1))/0.002) + 1;
Unrecognized function or variable 'Time'.
Xgrouped = accumarray(idx, X(:), [], @(vals){vals});
Ygrouped = accumarray(idx, Y(:), [], @(vals){vals});
Tgrouped = accumarray(idx, Time(:), [], @(vals){vals});
M
M 2023 年 10 月 2 日
@Walter Roberson Dyuman solution worked with me,but I am wondered why your solution didn't work

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by