Multiplication of 2 3d matrices

1 回表示 (過去 30 日間)
BdS
BdS 2019 年 5 月 14 日
編集済み: Andrei Bobrov 2019 年 5 月 14 日
Hi,
I have got 2 3d matrices: NormClassRetPf(dates,factors,classes) and TotWeightsPf(dates,factors,classes).
nDates=51
nFactors=19
nClasses=10
For each date I would like first to multiply (element wise) the first, second, third until nFactors rows of NormClassRetPf withTotWeightsPf and then sum the element wise multiplication. At the end I should get a 2-d matrix FactorRetPf(dates,factors) or FactorRetPf(factors,dates).
I tried this code:
for d=1:nDates
FactorRetPf(:,d)=nansum(TotWeightsPf(d,:,:).*NormClassRetPf(d,:,:));
FactorRetBM(:,d)=nansum(TotWeightsBM(d,:,:).*NormClassRetBM(d,:,:));
end
Do you know a more elegant way of doing this?
  2 件のコメント
Jan
Jan 2019 年 5 月 14 日
It is correct that the wanted outputs FactorRetPf(dates,factors) and FactorRetPf(factors,dates) have the indices in different order?
BdS
BdS 2019 年 5 月 14 日
Both 3d matrices have the order of (nDates,nFactors,nClasses) and the desire output would be FactorRetPf is (nDates,nFactors). A the end I should get per date (first dim) the daily performance of each factor portfolio (second dim).

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

回答 (2 件)

Andrei Bobrov
Andrei Bobrov 2019 年 5 月 14 日
編集済み: Andrei Bobrov 2019 年 5 月 14 日
FactorRetPf = nansum(TotWeightsPf.*NormClassRetPf,3)';
FactorRetBM = nansum(TotWeightsBM.*NormClassRetBM,3)';
  2 件のコメント
BdS
BdS 2019 年 5 月 14 日
thank you for your answer.
I get FactorRetPf of dimensions 190x51. Acutally I should only get 19x50.
I would like to element wise multiply each row of the both matrices which arises vom TotWeightsPf(dateX,:,:) and NormClassRetPf(dateX,:,:) and sum the multiplication for each row. By doing so, I should get FactorRetPf(nFactors,nDates).
Thank you for your feedback.
Andrei Bobrov
Andrei Bobrov 2019 年 5 月 14 日
I'm fixed.

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


Jan
Jan 2019 年 5 月 14 日
FactorRetPf = permute(nansum(TotWeightsPf .* NormClassRetPf, 2), [3,1,2]);
FactorRetBM = permute(nansum(TotWeightsBM .* NormClassRetBM, 2), [3,1,2]);
  2 件のコメント
BdS
BdS 2019 年 5 月 14 日
By doing so I get FactorRetPf of dimensions 10x51. I actually should get 51x19. Do you have any hint?
Jan
Jan 2019 年 5 月 14 日
編集済み: Jan 2019 年 5 月 14 日
The details in the question and in the code you have posted are confusing. But you can simply try it by your own.
  1. Multiply the arrays
  2. Create the sum over the wanted dimension
  3. Apply permute, transpose or reshape to ge the wanted output, if needed.
Maybe all you want to do is:
nansum(TotWeightsPf .* NormClassRetPf, 3)
If not adjust the parameters in:
FactorRetPf = permute(nansum(TotWeightsPf .* NormClassRetPf, 2), [3,1,2]);
% ^ ^ ^ ^

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

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by