Finding the mean at each interval
3 次查看(过去 30 天)
显示 更早的评论
Hi everyone,
I have data containing 2 columns; Temperature and V. Temp contain repititive temperature values and V contains (2,3 and 4). What I want is that for each 1°C temperature I want the mean of values in V. For example at 12°C, a number of values (2,2,3,4,4,2,2) fall in this inteval (12°C), so, therfore at 12°C = 2.7. Any suggestions how to do this? Data is attached. Thank you
0 个评论
回答(2 个)
Mathieu NOE
2021-9-22
hello
my 2 cents suggestion - see if for the empty intervals you prefer naN output (red dots) or you prefer interpolated data (black dots)
plot
code :
clc
clearvars
Tab = readtable('Data.xlsx') ;
T = Tab.Temp ;
V = Tab.V ;
[Ts,ind] = sort(T); % sort T in ascending order
Vs = V(ind);
minT = floor(min(Ts));
maxT = ceil(max(Ts));
Tnew = minT-0.5:1:maxT+0.5; % interval like -10.5 to 10.5
Tplot = minT:1:maxT; % corresponding center value (10)
for ci =1:length(Tnew)-1
ind = find(Ts>=Tnew(ci) & Ts<=Tnew(ci+1));
if ~isempty(ind)
Vmean(ci) = mean(Vs(ind));
else
Vmean(ci) = NaN;
end
end
% optionnal : remove NaN (missing) values by pchip interpolation
Vmean2 = interp1(Tplot,Vmean,Tplot,'pchip');
figure(1)
plot(Ts,Vs,'b',Tplot,Vmean,'-dr',Tplot,Vmean2,'*k');
legend('raw data (sorted)','averaged (including NaNs)','averaged (interpolated)');
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Point Cloud Processing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!