フィルターのクリア

Splitting a vector into vectors of different length

9 ビュー (過去 30 日間)
Mojtaba
Mojtaba 2014 年 7 月 16 日
編集済み: Alfonso Nieto-Castanon 2014 年 7 月 17 日
I want to split a vector with 90 arrays to 5 vectors with different lengths. The length of each vector is determined according to a normalized length like this: nl=[0.1642 .1516 .1259 .5583] Therefore the length of each vector is length=90*[0.1642 .1516 .1259 .5583]. But these lengths are not integer, and if i try to round this the length of will not equal to 90, it might be 89, 90, 91 or even 92 due to round of error.                        I would appreciate if anyone could assist me to write a code for this.
  2 件のコメント
Geoff Hayes
Geoff Hayes 2014 年 7 月 16 日
You may need to provide an example. What do you mean by split a vector with 90 arrays?
Alfonso Nieto-Castanon
Alfonso Nieto-Castanon 2014 年 7 月 17 日
I am guessing you mean "a vector with 90 elements" instead of "a vector with 90 arrays"?

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

採用された回答

Alfonso Nieto-Castanon
Alfonso Nieto-Castanon 2014 年 7 月 16 日
編集済み: Alfonso Nieto-Castanon 2014 年 7 月 17 日
This is interesting, perhaps something like this would do (this implements a greedy correction of your initial estimate of the desired lengths of each segment until the segment lengths add-up to the proper value):
nl = [0.1642 .1516 .1259 .5583]; % normalized lengths of each segment
N = 90; % total length of data to segment
n = round(nl*N); % initial lengths of each segment
while sum(n)~=N % if lengths do not add-up to N
d = sign(N-sum(n)); % direction of change
[~,i] = min(abs(n+d-N*nl)); % best segment to modify
n(i) = n(i)+d; % change that segment length
end
Then you can simply split your original vector using mat2cell:
x = randn(1,90);
y = mat2cell(x, 1, n);

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by