Reputation: 8463
So here is what I have :
an index : [3;4;5;6;7;8;9]
I have a vector that corresponds to that index :
inVector = [10;20;30;40;50;60;70;80;90]
What I want is that I get the mean of every 3 rows in the invector. Hence,
outVector = mean(inVector[index - 2 : index]);
Hence, outVector now has [20;30;40;50;60;70;80];
Upvotes: 0
Views: 58
Reputation: 112669
For arbitrary index
, you can do it using cumsum
:
N = 3;
aux = cumsum([0; inVector(:)]);
result = (aux(index+1)-aux(index-N+1))/N;
If index
always consists of consecutive indices, it's easier to use conv
(since a moving average is a convolution with a rectangular window):
N = 3;
result = conv(inVector,ones(1,N)/N,'valid');
Upvotes: 1