フィルターのクリア

Given a vector of assorted positive integers, how to create a vector with the means of every 2 integers inserted between each other?

1 回表示 (過去 30 日間)
I've managed to take the mean of the whole vector with the mean function, but this doesn't seem like the right path to take. I am thinking of: v= 2 6 8 3 1 9 4 5 7 m1= v(1,2:9) m2= [m1,v(end)] m3= [v;m2] vmeans=mean(m2)
there are quite a few intermediate steps just to get to the vector of the means by themselves.
any answers to get to these values better or how to insert those values in between each original integer are much appreciated
thanx

採用された回答

David Young
David Young 2011 年 6 月 9 日
result = interp1(v, linspace(1, length(v), length(v)*2-1), 'linear')
  2 件のコメント
Adam Quintero
Adam Quintero 2011 年 6 月 10 日
this looks like an elegant solution, and i want to make sure i understand the process. I see the seed vector (v) and the part that defines "the first and second number along (v)" in "...length(v),length(v)*2-1),.....". after i plug in some numbers im getting the math to work out. i am starting to see how i need to break these problems down, thank you for the elegant solution, this points me in the right direction.
David Young
David Young 2011 年 6 月 12 日
Yes, to understand this it may help to note that, for example
linspace(1, 3, 5)
gives
[1 1.5 2 2.5 3]
- that is, the points in the index space of v for which you want to find values.

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

その他の回答 (3 件)

David Young
David Young 2011 年 6 月 9 日
result(1:2:2*length(v)-1) = v;
result(2:2:2*(length(v)-1)) = conv(v, [1 1]/2, 'valid')

Andrei Bobrov
Andrei Bobrov 2011 年 6 月 9 日
m2 = v([2:end,end]);
m3 = [v;m2];
vmeans = mean(m2);
EDIT
vout = reshape([v;conv(v,[1 1],'valid')/2 0],1,[]);
vout = vout(1:end-1);
more only it case
vout = interp1(1:length(v),v,1:.5:length(v));
  4 件のコメント
Adam Quintero
Adam Quintero 2011 年 6 月 9 日
vmean1-2 is the mean betweeen the first and second number in the original vector. i am trying to set this up so that the mean is in between the values used to calculate it. i want to add a row vector value in between the values of a row vector. the numbers to add are the means of the two numbers before and after it
David Young
David Young 2011 年 6 月 9 日
It's more efficient to divide the mask in the convolution by 2, rather than dividing the result of the convolution by 2.
linspace is preferable to using the colon operator if the output needs to be a definite length and the increment is not an integer.

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


David Young
David Young 2011 年 6 月 9 日
v= [2 6 8 3 1 9 4 5 7]; % data
m = (v(1:end-1)+v(2:end))/2;
t = [v; [m 0]];
t = t(:);
result = t(1:end-1).'

カテゴリ

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