Finding the mean at each interval

4 views (last 30 days)
Zhou Ci
Zhou Ci on 22 Sep 2021
Answered: Mathieu NOE on 22 Sep 2021
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

Answers (2)

KSSV
KSSV on 22 Sep 2021
Edited: KSSV on 22 Sep 2021
T = readtable('https://in.mathworks.com/matlabcentral/answers/uploaded_files/746119/Data.xlsx') ;
Temp = T.Temp ;
V = T.V ;
t0 = 10; t1 = 12 ; % Intervel
idx = Temp > t0 & Temp < t1 ;
iwant = mean(V(idx))
iwant = 2.6726
  3 Comments
Zhou Ci
Zhou Ci on 22 Sep 2021
I want values (a complete column vector at each temp interval): Minimun value of temp is -72.95 and maximum is 37.45.
So at each temp interval I want a mean of V.

Sign in to comment.


Mathieu NOE
Mathieu NOE on 22 Sep 2021
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)');

Categories

Find more on Tables in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!