Mean and median at each interval

조회 수: 4 (최근 30일)
IMC
IMC 2021년 9월 30일
댓글: IMC 2021년 9월 30일
Hello,
My excel file has 3 columns representing Temperature (in Kelvin), Volume and Mass. At each 1°C interval I want a mean of Volume and a median of Mass. The data is from hdf file. So, basically at each temperature interval there are alot of pixels of Volume that fall at one interval (and I just need one value i.e. mean) and same goes for Mass. So, result should be something like;
Temp Volume Mass
-10 7 32
23 8 21
Data is attached. I just don't understand how to use findgroups function here. Any help would be highly appreciated. Thank you.
P.S. Exclude all the pixels with 0 and 1 value from Volume.

채택된 답변

Konrad
Konrad 2021년 9월 30일
편집: Konrad 2021년 9월 30일
Hi
data = readtable('Data.xlsx'); % read data
data = data(~ismember(data.Volume,[0 1]),:); % exclude rows where volume=0 or volume=1
[G,grpTemp] = findgroups(round(data.Temp)); % findgroups for rounded temps, grpTemp contains the temperature for each group
meanVol = splitapply(@mean,data.Volume,G); % mean volume
medianMass = splitapply(@median,data.Mass,G); % median mass
table(grpTemp, meanVol, medianMass) % display results
Best, K.
  댓글 수: 3
Konrad
Konrad 2021년 9월 30일
Hi,
the temperature is rounded and then grouped, all temps between 301.5 and 302.4 will fall in one group.
If you want values between 302.0 and 302.9 to fall into one group you can use floor() instead of round():
[G,grpTemp] = findgroups(floor(data.Temp));
IMC
IMC 2021년 9월 30일
Got it, Thanks alot!

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

추가 답변 (1개)

Kevin Holly
Kevin Holly 2021년 9월 30일
편집: Kevin Holly 2021년 9월 30일
Data = importfile('Data.xls')
% Histogram
figure
histogram(Data.Temp,[floor(min(Data.Temp)):1:ceil(max(Data.Temp))]);
xlabel('Temperature ({\circ}C)')
ylabel('frequency')
title('Histogram')
% Preallocate variables
mean_Volume = zeros(size(Temp_range));
median_Mass= zeros(size(Temp_range));
Temp_range = min(floor(Data.Temp)):1:max(floor(Data.Temp));
for i = 1:1:length(Temp_range)
mean_Volume(i) = mean(Data.Volume(floor(Data.Temp)==Temp_range(i)),'omitnan');
median_Mass(i) = median(Data.Mass(floor(Data.Temp)==Temp_range(i)),'omitnan');
end
% Mean Volume vs Temperature (*K)
figure
plot(Temp_range,mean_Volume)
xlabel('Temperature ({\circ}K)')
ylabel('Mean Volume (units)')
title('Mean Volume vs Temperature ({\circ}K)')
% Median Mass vs Temperature (*K)
figure
plot(Temp_range,median_Mass)
xlabel('Temperature ({\circ}K)')
ylabel('Median Mass (g)')
title('Median Mass vs Temperature ({\circ}K)')
% Create Table
table(Temp_range',mean_Volume',median_Mass')
function Data = importfile(workbookFile, sheetName, dataLines)
%IMPORTFILE Import data from a spreadsheet
% DATA = IMPORTFILE(FILE) reads data from the first worksheet in the
% Microsoft Excel spreadsheet file named FILE. Returns the data as a
% table.
%
% DATA = IMPORTFILE(FILE, SHEET) reads from the specified worksheet.
%
% DATA = IMPORTFILE(FILE, SHEET, DATALINES) reads from the specified
% worksheet for the specified row interval(s). Specify DATALINES as a
% positive scalar integer or a N-by-2 array of positive scalar integers
% for dis-contiguous row intervals.
%
% Example:
% Data = importfile("C:\Users\kevinh\Downloads\Data.xlsx", "Sheet1", [1, 3399]);
%
% See also READTABLE.
%
% Auto-generated by MATLAB on 30-Sep-2021 03:26:17
%% Input handling
% If no sheet is specified, read first sheet
if nargin == 1 || isempty(sheetName)
sheetName = 1;
end
% If row start and end points are not specified, define defaults
if nargin <= 2
dataLines = [1, 3399];
end
%% Set up the Import Options and import the data
opts = spreadsheetImportOptions("NumVariables", 3);
% Specify sheet and range
opts.Sheet = sheetName;
opts.DataRange = "A" + dataLines(1, 1) + ":C" + dataLines(1, 2);
% Specify column names and types
opts.VariableNames = ["Temp", "Volume", "Mass"];
opts.VariableTypes = ["double", "double", "double"];
% Import the data
Data = readtable(workbookFile, opts, "UseExcel", false);
for idx = 2:size(dataLines, 1)
opts.DataRange = "A" + dataLines(idx, 1) + ":C" + dataLines(idx, 2);
tb = readtable(workbookFile, opts, "UseExcel", false);
Data = [Data; tb]; %#ok<AGROW>
end
end

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by