How to split a vector in to sub vectors?

126 ビュー (過去 30 日間)
Jayanta Deb
Jayanta Deb 2017 年 8 月 16 日
コメント済み: Pierre Mathieu 2022 年 10 月 10 日
I have a vector 'v' of size 8812x1,I want to split it into 20(or known length n) sub-vectors. eg. v = {1,2,3,...,8812}
I wish to see:
v1={1,2,3,...,20}
v2={21,22,23,...,41}
v3 = {42,43,44,...,62}
...
v20 = {...} % the last vector doesn't matter if it can have full 20 or not, it can / might get lesser number as its not fully possible to get a whole 20 elements divide equally with the size of the vector but obviously the vector v1 till v19 must have 20 elements.
Then I want to calculate the median from each sub divided vectors.
So it should be like m1 = median(v1), m2 = median(v2)... m20=median(v20)
I would appreciate if it is done with a loop
  1 件のコメント
Stephen23
Stephen23 2017 年 8 月 16 日
編集済み: Stephen23 2022 年 9 月 8 日
"I wish to see:"
v1 = {1,2,3,...,20}
v2 = {21,22,23,...,41}
v3 = {42,43,44,...,62}
DO NOT DO THIS.

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

採用された回答

Stephen23
Stephen23 2017 年 8 月 16 日
編集済み: Stephen23 2017 年 8 月 16 日
Simpler:
>> v = rand(8812,1);
>> b = 20; % block size
>> n = numel(v);
>> c = mat2cell(v,diff([0:b:n-1,n]));
>> z = cellfun(@median,c);
Or if you really want to use a loop, replace the last line with:
z = NaN(numel(c),1);
for k=1:numel(c)
z(k) = median(c{k});
end
  2 件のコメント
Md Shahriar Islam
Md Shahriar Islam 2018 年 10 月 12 日
Hi, I would like to know what to do if block size, b is unknown. That is b is dependent on v.
Redouane Bouchou
Redouane Bouchou 2020 年 6 月 21 日
thank you very much you are a genius

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

その他の回答 (2 件)

José-Luis
José-Luis 2017 年 8 月 16 日
v = rand(8812,1);
n = 20;
dummy = nan(20,ceil(numel(v)./n));
dummy(1:numel(v)) = v;
result = nanmedian(dummy)
  2 件のコメント
Salih Bulut
Salih Bulut 2022 年 9 月 8 日
This is the fastest code found here.
Pierre Mathieu
Pierre Mathieu 2022 年 10 月 10 日
Yes, very good!

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


ES
ES 2017 年 8 月 16 日
編集済み: ES 2017 年 8 月 16 日
v = [1:1:8812];
window_size = 20;
iCount = size(v,2);
iStartIDx = [1:window_size :iCount];
for iLoop=1:length(iStartIDx)-1
segment{iLoop} = v(iStartIDx(iLoop):iStartIDx(iLoop+1)-1);
med{iLoop} = median(segment{iLoop});
end
segment{iLoop+1} = v(iStartIDx(iLoop+1):end);
med{iLoop+1} = median(segment{iLoop+1});
  3 件のコメント
Stephen23
Stephen23 2017 年 8 月 16 日
編集済み: Stephen23 2017 年 8 月 16 日
@J Smith: the code could be improved:
  • those output arrays should be preallocated.
  • note that square brackets serve no purpose: [1:1:8812] is better written as 1:1:8812 (see the editor warning message and this discussion).
  • using median as a variable name is a bad idea.
José-Luis
José-Luis 2017 年 8 月 16 日
編集済み: José-Luis 2017 年 8 月 16 日
This is rather wrong. On top of being incomplete, as pointed out by Jayanta:
There is no pre-allocation, a performance killer.
median is used both as a variable name and as a function. That's bound to be problematic.
Edit: latest comment was valid before answer being edited.

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by