How to average a specific number elements which have the same index value

6 views (last 30 days)
Hi there,
I am trying to create a function which finds the average of a number of elements in columns that have a similar index value in another column, in my attempt below I can find the average of say last two elements when they are ordered sequentially however am not sure how to find the average of say prior two elements only when the index values match.
function avgprevious2elements(a)
[nrows, ncolumns] = size(a);
new = zeros(nrows,ncolumns);
for i=3:nrows
for j=3:ncolumns
new(i,j)= (a(i-1,j) + a(i-2,j))/2
end
end
Any assistance you are able to offer is greatly appreciated.

Answers (0)

Community Treasure Hunt

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

Start Hunting!