How to find the mean of a matrice based on a value in another matrice
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Adi Purwandana
le 8 Oct 2024
Commenté : Star Strider
le 9 Oct 2024
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
0 commentaires
Réponse acceptée
Star Strider
le 8 Oct 2024
This returns all of the monthly means for all the months —
LD = load('data_x.mat')
T1 = table(LD.month, LD.sa, 'VariableNames',{'month','sa'})
[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'})
.
4 commentaires
Plus de réponses (1)
Sameer
le 8 Oct 2024
Modifié(e) : Sameer
le 8 Oct 2024
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!
Voir également
Catégories
En savoir plus sur Matrix Computations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
