MATLAB Answers

Finding the mean at each interval

조회 수: 4(최근 30일)
Zhou Ci
Zhou Ci 2021년 9월 22일
답변: Mathieu NOE 2021년 9월 22일
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

답변(2개)

KSSV
KSSV 2021년 9월 22일
편집: KSSV 2021년 9월 22일
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
Zhou Ci
Zhou Ci 2021년 9월 22일
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.

댓글을 달려면 로그인하십시오.


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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by