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

4 ビュー (過去 30 日間)
Adi Purwandana
Adi Purwandana 2024 年 10 月 8 日
コメント済み: Star Strider 2024 年 10 月 9 日
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

採用された回答

Star Strider
Star Strider 2024 年 10 月 8 日
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 件のコメント
Adi Purwandana
Adi Purwandana 2024 年 10 月 9 日
Perfect as always! Thank you!
Star Strider
Star Strider 2024 年 10 月 9 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (1 件)

Sameer
Sameer 2024 年 10 月 8 日
編集済み: Sameer 2024 年 10 月 8 日
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!

カテゴリ

Help Center および File ExchangeMatrix Computations についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by