Finding the mean at each interval

3 Ansichten (letzte 30 Tage)
Zhou Ci
Zhou Ci am 22 Sep. 2021
Beantwortet: Mathieu NOE am 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

Antworten (2)

KSSV
KSSV am 22 Sep. 2021
Bearbeitet: KSSV am 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 Kommentare
KSSV
KSSV am 22 Sep. 2021
Edited the code
Zhou Ci
Zhou Ci am 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.

Melden Sie sich an, um zu kommentieren.


Mathieu NOE
Mathieu NOE am 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)');

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by