How do I average data to produce 1-m interval bins?

8 views (last 30 days)
I've got arrays (from text files) of profiles, 1 -> 200m, and back up to the surface, with 40 variables (columns). The data are acquired continuously over depth. I'm looking for a routine that will give me depths at 1-m intervals across all 40 columns, so I end up with arrays of (1:200,1:40). An additional problem is that the profiles vary slightly in near-surface and bottom depths (e.g., 3-197, 2-198, etc), making it a challenge to create arrays of depth v. some variable across all profiles for contouring purposes.
  7 Comments
John
John on 16 Jul 2015
Yeah, I noticed that 36x19. Not sure what happened. FWIW, depth is column 18.
Star Strider
Star Strider on 16 Jul 2015
That shouldn’t affect my code. I changed it to account for the depth in Col #18, although it ended up in Col 17 in the output of my code, since I deleted Col #1 (a line counter) in the output. You can always put the depth in any column you want. Just change the other column assignments.
If somehow it does affect my code, post a section of your complete data set and I will change my code to accommodate your full-column data file.

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 15 Jul 2015
Assuming depth is in the second column read (Column ‘B’ in the Excel file):
[d,s,r] = xlsread('DataExtract.csv');
dep = d(:,2); % Depth Column
idep = fix(dep)-min(fix(dep))+1; % Create Integer Subscript Of Depths
vblm = d(:,3:end); % Rest Of Data
[idepu, ia, ic] = unique(idep); % Unique Depths & Indices
for k1 = 1:size(idepu,1)
depmean(k1,:) = [mean(vblm(ic==k1,:))]; % Means By Metre
end
depmean = [idepu+min(fix(dep))-1 depmean]; % Depth = Col #1
The ‘depmean’ assignment is the output. Column #1 is the depth, the other columns are the respective means at that depth. The depths are sorted in ascending order.
If depth is another column in the Excel file, change the ‘dep’ assignment column reference. The code will work without other modification. I experimented with non-loop possibilities, but none would work.
  9 Comments
anastasia pa
anastasia pa on 1 May 2020
for k1 = 1:size(idepu,1)
depmean(k1,:) = [mean(vblm(ic==k1,:))]; % Means By Metre
end
If idepu and vblm are cells how will be the above code for means by metre?
Thanks!!!

Sign in to comment.

More Answers (1)

Guillaume
Guillaume on 15 Jul 2015
Assuming that each row of your array is of the form:
%depth var1 var2 ... var40
%e.g:
data = [1.1 rand(1,40); %depth is 1.1
2.5 rand(1,40); %depth is 2.5
1.8 rand(1,40); %depth is 1.8
...
]
you can use discretize to find which rows to average together:
depths = 1:200
bins = discretize(data(:, 1), depths);
You can then use a for loop to average for each bin:
newdata = zeros(200, 40);
for depth = depths
newdata(depth, :) = mean(data(bins == depth, 2:41));
end
  2 Comments
John
John on 16 Jul 2015
Thanks, this looks like a simple solution. But unfortunately, I'm using R2014B, and discretize is not recognized.
Guillaume
Guillaume on 16 Jul 2015
The second return value of histc or the third of histcounts (can't remember if it was in 2014b) will give you the exact same result as discretize.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!