# How to perform the following data splitting?

13 views (last 30 days)

Show older comments

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

##### 0 Comments

### Accepted Answer

Dyuman Joshi
on 1 Oct 2023

%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'

##### 2 Comments

Walter Roberson
on 1 Oct 2023

### More Answers (1)

Walter Roberson
on 1 Oct 2023

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 Comments

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!