Split vector by parts with more evenly distributed length
4 ビュー (過去 30 日間)
古いコメントを表示
While my previous question got answered fine (and I did not specify it good enough), I'm looking for a way to split a vector so that the sizes of the subvectors are more evenly distributed. Let me explain with the following example.
So again let's say I have the following vector
V = 1:2184
P = [floor(length(V/127)) & ceil(length(V/127))] %[17 18]
Now I want to split the vector somewhat more distributed, so:
sv1 sv2 sv3 sv4 sv5 ... sv123 sv124 sv125 sv126 sv127 %subvector;
Vbad = [17 17 17 17 17 ... 17 20] %sv length; don't want this
Vgood = [17 17 17 17 17 ... 17 17 18 18 18] %sv length; looking for this; always end up with 127 subvectors
Thanks in advance.
1 件のコメント
Ameer Hamza
2018 年 4 月 26 日
It appears that you are not even running the code before posting here. Have you computed the output of the following line
P = [floor(length(V/127)) & ceil(length(V/127))] %[17 18]
what you actually meant is
P = [floor(length(V)/127)];
採用された回答
Ameer Hamza
2018 年 4 月 26 日
As @dbp pointed out, the problem still isn't well defined, but based on your example the answer will only contain P or P+1 elements.
V = 1:2184;
numberPartition = 127;
P = floor(length(V)/numberPartition);
L = length(V) - numberPartition*P;
partitionElements = [P*ones(1, numberPartition-L) (P+1)*ones(1, L)];
partitions = mat2cell(V, 1, partitionElements);
その他の回答 (1 件)
dpb
2018 年 4 月 25 日
Consider the following...
>> for k=fix(sqrt(L)):-1:2,if rem(L,k)==0,disp([L,k,L/k]),end,end
2184 42 52
2184 39 56
2184 28 78
2184 26 84
2184 24 91
2184 21 104
2184 14 156
2184 13 168
2184 12 182
2184 8 273
2184 7 312
2184 6 364
2184 4 546
2184 3 728
2184 2 1092
>>
gives you every factor evenly divisible into your vector length; one of these choices (and only one of these) will be the value that is exactly even. If you have some other criteria on how many divisions or a target size, then you may have to heuristically pick another value.
It still isn't well-enough defined to know what are hard requirements vis a vis desires.
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!