How can I calculate average values of one data according to specific indexes of same size?
Show older comments
Hi all,
I am intending to calculate average values of density(832695x1 double) according to depth indexes(832695x1 double), in order to obtain one single value of density for each 1 unit depth - elucidated by the table below.

I tried to find a solution applying the following loop; however the results have been not what I was expecting.
if depth_ind=round(depth);
for i=1:1:length(dens)-8
if depth_ind(i)==depth_ind(i+8)
M(i)=(dens(i)+dens(i+1)+dens(i+2)+dens(i+3)+dens(i+4)+dens(i+5)+dens(i+6)+dens(i+7)+dens(i+8))/9;
elseif depth_ind(i)==depth_ind(i+7)
M(i)=(dens(i)+dens(i+1)+dens(i+2)+dens(i+3)+dens(i+4)+dens(i+5)+dens(i+6)+dens(i+7))/8;
elseif depth_ind(i)==depth_ind(i+6)
M(i)=(dens(i)+dens(i+1)+dens(i+2)+dens(i+3)+dens(i+4)+dens(i+5)+dens(i+6))/7;
elseif depth_ind(i)==depth_ind(i+5)
M(i)=(dens(i)+dens(i+1)+dens(i+2)+dens(i+3)+dens(i+4)+dens(i+5))/6;
elseif depth_ind(i)==depth_ind(i+4)
M(i)=(dens(i)+dens(i+1)+dens(i+2)+dens(i+3)+dens(i+4))/5;
elseif depth_ind(i)==depth_ind(i+3)
M(i)=(dens(i)+dens(i+1)+dens(i+2)+dens(i+3))/4;
elseif depth_ind(i)==depth_ind(i+2)
M(i)=(dens(i)+dens(i+1)+dens(i+2))/3;
elseif depth_ind(i)==depth_ind(i+1)
M(i)=(dens(i)+dens(i+1))/2;
else
M(i)=dens(i);
end
end
How could I do that properly?
Thank you for your time,
Accepted Answer
More Answers (1)
Andrei Bobrov
on 30 Jun 2015
Edited: Andrei Bobrov
on 30 Jun 2015
n = xlsread('path_and_name_your_file_data.xlsx');
out1 = accumarray(n(:,1),n(:,2),[],@mean);
out = [n(:,1),out1(n(:,1))];
1 Comment
Gustavo Oliveira
on 30 Jun 2015
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!