Multiplication of 2 3d matrices
1 次查看(过去 30 天)
显示 更早的评论
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
2019-5-14
It is correct that the wanted outputs FactorRetPf(dates,factors) and FactorRetPf(factors,dates) have the indices in different order?
回答(2 个)
Andrei Bobrov
2019-5-14
编辑:Andrei Bobrov
2019-5-14
FactorRetPf = nansum(TotWeightsPf.*NormClassRetPf,3)';
FactorRetBM = nansum(TotWeightsBM.*NormClassRetBM,3)';
Jan
2019-5-14
FactorRetPf = permute(nansum(TotWeightsPf .* NormClassRetPf, 2), [3,1,2]);
FactorRetBM = permute(nansum(TotWeightsBM .* NormClassRetBM, 2), [3,1,2]);
2 个评论
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.
- Multiply the arrays
- Create the sum over the wanted dimension
- 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 Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!