How to do Multichannel statistical analysis?
17 views (last 30 days)
Show older comments
I just need an example:
%% Load data
% Lab says use readtable, then convert to array
T = readtable("Section2.csv");
data = table2array(T);
% Assumption: column 1 is time/index, columns 2:11 are the 10 subjects
X = data(:,2:11); % Nx10 matrix (each column = one subject)
%% =========================
% [3.2] Central tendency + dispersion for each subject
% mean, median, range, std (one value per subject/column)
% =========================
mean_data = mean(X, 'omitnan'); % 1x10
median_data = median(X, 'omitnan'); % 1x10
% Use max-min for range (robust if MATLAB "range" gives issues)
range_data = max(X, [], 1) - min(X, [], 1); % 1x10
std_data = std(X, 0, 'omitnan'); % 1x10 (0 = sample std)
%% =========================
% [3.2] Find which subject has max/min for each metric
% Lab asks to do this by code (not by looking)
% =========================
[max_mean, subj_max_mean] = max(mean_data);
[min_mean, subj_min_mean] = min(mean_data);
[max_median, subj_max_median] = max(median_data);
[min_median, subj_min_median] = min(median_data);
[max_range, subj_max_range] = max(range_data);
[min_range, subj_min_range] = min(range_data);
[max_std, subj_max_std] = max(std_data);
[min_std, subj_min_std] = min(std_data);
% Display summary
fprintf('\n=== Subject Extremes (Subjects 1-10) ===\n');
fprintf('Mean : max = %.2f (Subject %d), min = %.2f (Subject %d)\n', ...
max_mean, subj_max_mean, min_mean, subj_min_mean);
fprintf('Median : max = %.2f (Subject %d), min = %.2f (Subject %d)\n', ...
max_median, subj_max_median, min_median, subj_min_median);
fprintf('Range : max = %.2f (Subject %d), min = %.2f (Subject %d)\n', ...
max_range, subj_max_range, min_range, subj_min_range);
fprintf('Std Dev: max = %.2f (Subject %d), min = %.2f (Subject %d)\n', ...
max_std, subj_max_std, min_std, subj_min_std);
%% =========================
% [3.2] Time spent > (mean + 20 bpm), in HOURS
% Lab says each row = 5 seconds
% =========================
function hours = time_over(colVec, meanVal)
% colVec = one subject's HR time series (Nx1)
% meanVal = mean HR for that subject
count = sum(colVec > (meanVal + 20)); % number of samples above threshold
hours = (count * 5) / 3600; % 5 s/sample -> hours
end
% Compute for all 10 subjects
hours_over = zeros(1,10);
for s = 1:10
hours_over(s) = time_over(X(:,s), mean_data(s));
end
% Optional: display results neatly
fprintf('\n=== Time above (mean + 20 bpm) ===\n');
for s = 1:10
fprintf('Subject %d: %.3f hours\n', s, hours_over(s));
end
%% Optional: put everything into a table (nice for report/checking)
subject_id = (1:10)';
resultsTbl = table(subject_id, mean_data', median_data', range_data', std_data', hours_over', ...
'VariableNames', {'Subject','MeanHR','MedianHR','RangeHR','StdHR','HoursAboveMeanPlus20'});
disp(resultsTbl);
1 Comment
Image Analyst
ungefär en timme ago
That looks like an example. You just need "Section2.csv" which you can presumably get from wherever you got the example code for it.
Answers (1)
dpb
on 20 Feb 2026 at 15:16
Edited: dpb
ungefär 12 timmar ago
% Lab says use readtable, then convert to array
T = readtable("Section2.csv");
data = table2array(T);
% Assumption: column 1 is time/index, columns 2:11 are the 10 subjects
X = data(:,2:11); % Nx10 matrix (each column = one subject)
That may be what the lab says, but it isn't the effective way to use MATLAB -- if going to use arrays, then it would be more efficient to read the data that way to begin with instead of having two copies of the same thing in a table and another array.
tStat=groupsummary(T,[],{@mean,@median,@range,@std},[2:11]);
will return the table with the desired statistics.
I don't follow the comment about the robustness of range, as implemented it ignores NaN elements automatically.
If there are NaN values scattered around in the data set and it isn't wanted to discard observations that are not complete for all subjects, then it may be necessary to use anonymous functions that set the 'omitnan' parameter to pass as the function handles for the various statistics wanted instead of the builtin version.
The builtin nanXXX versions could be used directly for the same effect for those that are available which do include the specific statistics listed--
tStat=groupsummary(T,[],{@nanmean,@nanmedian,@range,@nanstd},[2:11]);
Note that the [] second argument to groupsummary tells it that the computations are over the whole dataset, not using any grouping variables.
Other than that, don't really know what the question wanting to be answered is.
0 Comments
See Also
Categories
Find more on Classification Ensembles in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!