MATLAB: I need help finding the mean/avg of the data set

avergemean

I need help setting a loop to find the mean/avg of my data set. The data set I am looking at is COPNET_x and COPNET_z each of these data contain 3 trials for each subject(49) and the data length output is 6000. What i want to do is to aveg the result of these 3 trials for each subject so i can reduce my 6000 data length. Everything in my code works fine. just need help setting up this mean/avg part.
BMI_numbers = [24.97;34.7;19.01;23.59;18.03;27.4;28.92;19.22;21.42;23.45;21.04;19.55;21.34;32.64;27.75;27.38;26.06;31.48;24.34;22.97;21.14;26.05;22.08;25.19;21.74;18.83;21.04;24.06;27.44;22.77;28.22;30.61;26.07;23.03;25.96;28.52;23.11;31.6;25.66;18.73;28.74;21.5;30.3;28.46;19.95;34.29;21.82;28.95;24.34];
BMI_labels = {'Obese';'Non-Obese'};
subject_numbers = [1:49];
trial_numbers = [1:3];
trial_labels = {'OR1','OR2','OR3'};
counter = 0;
fs = 49;
%% Set up loop structure
for sub = subject_numbers
for trial = trial_numbers
counter = counter + 1;
%% Construct file name and full path
if sub<10
subject_label = ['0',int2str(sub)];
else
subject_label = [int2str(sub)];
end
filename_grf = ['PDS',subject_label,'OR',int2str(trial),'grf.txt'];
fullpath_grf = [maindirectory,filename_grf];
%% Load data
if exist(fullpath_grf,'file')~=0
data_grf = table2array(readtable(fullpath_grf));
%% Analyze data
COPNET_x = data_grf(:,11);
COPNET_z = data_grf(:,13);
%%%%%%% How can i find the mean/avg for each subject(3 trials) inside my COPNET data %%%%%%%%%%%%%%%%%%%%%%%%%%%
for i =1:3
av(:,i) = mean(COPNET_x);
end
%GRFNET_x = data_grf(:,5);
%GRFNET_y = data_grf(:,7);
% BMI < 25 = normal , BMI >25 = overwieght
n = length(COPNET_x);
Rx = max(COPNET_x)- min(COPNET_x); %sway range in AP
Rz = max(COPNET_z)-min(COPNET_z); % sway range in ML
V_AP = sum(abs(diff(COPNET_x)).*fs)/n; % the average velocity in the AP
V_ML = sum(abs(diff(COPNET_z)).*fs)/n; % the average velocity in the ML
Velx_max = max(diff(COPNET_x).*fs); % the max velocity in the AP
Velz_max = max(diff(COPNET_z).*fs); % the max velocity in the ML
Vel_total = sum(sqrt(diff(COPNET_x).^2+diff(COPNET_z).^2).*fs)/n; % The total average velocity COP
Vmax_total = max(sqrt(diff(COPNET_x).^2 +diff(COPNET_z).^2).*fs)/n; % The max velocity COP displacement
results = [Rx Rz V_AP V_ML Velx_max Velz_max Vel_total Vmax_total];
%How to categorized my data results
%% Output data
data_output(counter,:) = [sub BMI_numbers(sub) trial results];
else
continue
end
end
end

Best Answer

  • I would use groupsummary for this.
    Keep your data in a table.
    We don't have your data to test with, but here's a simple example.
    load patients.mat
    data = table(Gender,Age,Weight,Height,Systolic,Diastolic,Location);
    % Compute the average of each Gender at each location (similar to each trial for each patient)
    avgTbl = groupsummary(data,["Gender","Location"],'mean',["Age","Height","Weight","Systolic","Diastolic"])
    avgTbl = 6x8 table
    Gender Location GroupCount mean_Age mean_Height mean_Weight mean_Systolic mean_Diastolic __________ _____________________________ __________ ________ ___________ ___________ _____________ ______________ {'Female'} {'County General Hospital' } 19 38 64.789 129.11 123.47 82.421 {'Female'} {'St. Mary's Medical Center'} 15 36.867 65.533 129.2 121 80 {'Female'} {'VA Hospital' } 19 38.105 65.211 132.84 120.68 81.895 {'Male' } {'County General Hospital' } 20 39.7 68.6 177.9 124.75 85.45 {'Male' } {'St. Mary's Medical Center'} 9 36.222 69.111 182.78 123 82.889 {'Male' } {'VA Hospital' } 18 39.389 70 182.33 123.44 84.389