How to find the mean of a matrice based on a value in another matrice

Hello everyone,
I have two matrices (attached file data_x.mat contains two matrices/parameters) namely 'month' and 'sa'. I want to get, the mean of 'sa' which correspond to values, for example of month = 9 only. Anyone knows how to handle this?
Thanks

 Accepted Answer

This returns all of the monthly means for all the months —
LD = load('data_x.mat')
LD = struct with fields:
month: [125818x1 double] sa: [125818x1 double]
T1 = table(LD.month, LD.sa, 'VariableNames',{'month','sa'})
T1 = 125818x2 table
month sa _____ ______ 9 34.532 9 34.54 9 34.545 9 34.544 9 34.542 9 34.543 9 34.544 9 34.555 9 34.542 9 34.541 9 34.541 9 34.54 9 34.532 9 34.519 9 34.527 9 34.544
[Um,~,ix] = unique(T1.month, 'stable');
mmeans = accumarray(ix, (1:numel(ix)).', [], @(x)mean(T1.sa(x)));
Monthly_Means = table(Um, mmeans, 'VariableNames',{'month','mean'})
Monthly_Means = 12x2 table
month mean _____ ______ 9 34.551 10 34.551 11 34.552 12 34.555 1 34.557 2 34.556 3 34.555 4 34.556 5 34.555 6 34.554 7 34.552 8 34.553
.

4 Comments

Perfect! Thank you! One more question if you don't mind. Could you please assist me to make box plot for this results, so that I can see what's the stdev, mean, median, etc. to investigate how large the variance of the value for each month?
Thank you!
I am not certain how much information you can get from the boxchart, so I added a new accumarray call to produce that information. If you have the Statistics and Machine Learning Toolbox, you can also experiment with the boxplot function. I use boxchart here (introduced in R2020a) because you should have it as part of core MATLAB.
Try this —
LD = load('data_x.mat')
LD = struct with fields:
month: [125818x1 double] sa: [125818x1 double]
T1 = table(LD.month, LD.sa, 'VariableNames',{'month','sa'})
T1 = 125818x2 table
month sa _____ ______ 9 34.532 9 34.54 9 34.545 9 34.544 9 34.542 9 34.543 9 34.544 9 34.555 9 34.542 9 34.541 9 34.541 9 34.54 9 34.532 9 34.519 9 34.527 9 34.544
[Um,~,ix] = unique(T1.month, 'stable');
monthdata = accumarray(ix, (1:numel(ix)).', [], @(x){[mean(T1.sa(x)), median(T1.sa(x)), std(T1.sa(x)), min(T1.sa(x)), max(T1.sa(x)), max(T1.sa(x))-min(T1.sa(x)), numel(T1.sa(x))]});
Results = array2table(cell2mat(monthdata), 'VariableNames',{'Mean','Median','StDev','Minimum','Maximum','Range','N'}, 'RowNames',compose('%3d',Um))
Results = 12x7 table
Mean Median StDev Minimum Maximum Range N ______ ______ ________ _______ _______ ______ _____ 9 34.551 34.553 0.015652 34.397 34.687 0.2908 9966 10 34.551 34.553 0.01478 34.388 34.663 0.2757 13392 11 34.552 34.553 0.012936 34.438 34.685 0.2475 12960 12 34.555 34.555 0.011521 34.434 34.697 0.263 13392 1 34.557 34.558 0.013831 34.367 34.733 0.367 13392 2 34.556 34.557 0.012418 34.469 34.666 0.1963 9724 3 34.555 34.555 0.014888 34.374 34.718 0.3433 8928 4 34.556 34.557 0.012806 34.443 34.669 0.2257 8640 5 34.555 34.556 0.013553 34.414 34.669 0.2552 8928 6 34.554 34.553 0.012718 34.452 34.693 0.2403 8640 7 34.552 34.551 0.01416 34.349 34.71 0.3613 8928 8 34.553 34.554 0.014175 34.345 34.69 0.3441 8928
% Monthly_Means = table(Um, mmeans, 'VariableNames',{'month','mean'})
figure
boxchart(T1.month, T1.sa, MarkerStyle='+', JitterOutliers='on', Notch='on')
grid
xticks(1:12)
xlabel('Months')
ylabel('Data')
title('Box Plot')
Jittering the outliers produces a ‘violin plot’ effect, and gives a better sense of their distribution.

Sign in to comment.

More Answers (1)

Hi Adi
To calculate the mean of 'sa' values corresponding to a specific 'month' (e.g., month = 9), you can follow the below approach:
% Load the data from the .mat file
data = load('data_x.mat');
% Extract the 'month' and 'sa' matrices
month = data.month;
sa = data.sa;
% Find indices where month is equal to 9
indices = find(month == 9);
% Extract the corresponding 'sa' values
sa_selected = sa(indices);
% Calculate the mean
mean_sa = mean(sa_selected);
fprintf('The mean of sa values for month = 9 is: %.2f\n', mean_sa);
I hope this helps!

Products

Release

R2022a

Community Treasure Hunt

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

Start Hunting!