How do I average data to produce 1-m interval bins?
8 次查看(过去 30 天)
显示 更早的评论
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 个评论
Star Strider
2015-7-16
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.
采纳的回答
Star Strider
2015-7-15
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 个评论
anastasia pa
2020-5-1
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!!!
更多回答(1 个)
Guillaume
2015-7-15
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
...
]
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 个评论
Guillaume
2015-7-16
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!