How to split a vector into unequal sections?

19 ビュー (過去 30 日間)
Dominik Mattioli
Dominik Mattioli 2017 年 5 月 24 日
コメント済み: Dominik Mattioli 2017 年 5 月 24 日
I know that some similar questions have previously been asked, but I think this problem might be a little unique. For some context, I have this vector V whose values never decrease (is there a word for this?) and whose min (1) and max (5) I always know.:
V = [1;1;2;2;2;2;3;4;4;5];
I'd like to somehow get the vertices for each "grouping" g (or some other format of distinction such as indexing) of V for like-numbers, i.e.
g1 = [1;1]
g2 = [2;2;2;2];
g3 = 3;
g4 = [4;4];
g5 = 5;
What I have right now seems ad hoc and mendable and I am not sure if it will be ideal for large data sets that I intend to apply this toward. I imagine an easier solution would simply obtain indices for each grouping. This is what I've done:
Vchange = logical(diff(V));
maxG = 5; % For this case.
Gidx = 1; % Begin first grouping with minimum.
G = cell(maxG,1); % Initialize output.
G{1} = V(1);
iter = length(Vchange); % Number of iterations.
for Vidx = 1:iter
if Vchange(i) % Vertex has changed;
Gidx = Gidx + 1; % continue with next vertex.
end
G{Gidx} = [G{Gidx},V(i)];
end

採用された回答

Stephen23
Stephen23 2017 年 5 月 24 日
編集済み: Stephen23 2017 年 5 月 24 日
>> V = [1;1;2;2;2;2;3;4;4;5];
>> D = diff(find([1;diff(V)~=0;1]));
>> C = mat2cell(V,D,1);
>> C{:}
ans =
1
1
ans =
2
2
2
2
ans =
3
ans =
4
4
ans =
5
You can use cell array indexing to access the numeric vectors in C.

その他の回答 (1 件)

Jan
Jan 2017 年 5 月 24 日
If you have a large data set, creating a bunch of variables can waste memory. Note that each array has an overhead of about 100 Byte. In addition all sub arrays contain the same value only, which is quite redundant. A smarter way of indexing might be better:
[B, N, Ind] = RunLength(A);
Now B contains the value, N the number of elements and Ind the index related to A. This takes much less memory and this might accelerate your code.
  1 件のコメント
Dominik Mattioli
Dominik Mattioli 2017 年 5 月 24 日
This is awesome, thank you! Do you have this in Python as well?

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

カテゴリ

Help Center および File ExchangePerformance and Memory についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by