- why is 20 in a2 and not in a3 ?
- why is 21 in a3 and not in a2 ?
How to split array into sub arrays?
8 次查看(过去 30 天)
显示 更早的评论
how to split principal vector "a" into sub vectors a1, a2, a3 and a4
exemple:
a=[1 3 5 8 11 12 15 17 18 19 20 21 24 29 31 32 33 34 35 36 38 39];
a1=[1 3 5 8];
a2=[11 12 15 17 18 19 20];
a3=[21 24 29];
a4=[31 32 33 34 35 36 38 39];
thnkx
3 个评论
采纳的回答
Dyuman Joshi
2024-1-30
编辑:Dyuman Joshi
2024-2-19
a = [1 3 5 8 11 12 15 17 18 19 20 21 24 29 31 32 33 34 35 36 38 39 69].';
%bin the data into groups of 10
k = findgroups(floor((a-1)/10));
z = accumarray(k, a, [], @(x) {x.'})
0 个评论
更多回答(1 个)
Matt J
2024-1-30
编辑:Matt J
2024-2-19
a = [1 3 5 8 11 12 15 17 18 19 20 21 24 29 31 32 33 34 35 36 38 39];
G=findgroups(discretize(a,[0:10:max(a)+10],'Include','right'));
z=splitapply(@(x){x}, a, G)
2 个评论
Dyuman Joshi
2024-2-19
splitapply() won't work if there is a gap in the data -
a = [1 3 5 8 11 12 15 17 18 19 20 21 24 29 31 32 33 34 35 36 38 39 69];
G=discretize(a,[0:10:max(a)+10],'Include','right');
z=splitapply(@(x){x}, a, G)
Matt J
2024-2-19
Small modification,
a = [1 3 5 8 11 12 15 17 18 19 20 21 24 29 31 32 33 34 35 36 38 39 69];
G=findgroups(discretize(a,[0:10:max(a)+10],'Include','right'));
z=splitapply(@(x){x}, a, G)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!