I have 2 matrices, Y and P, both of size m by n. I need to implement the following equation:
I'm having an issue with the index changing between j and l.
My initial try was:
eq = 2*Y.*Y - Y.*P - Y.*P
However, this is wrong of course since the multiplication would be with the same indices. I was thinking I would have to expand the matrices into a third dimension to overcome this problem with an extra index and then run some similar code for the equation, but I don't really understand how to perform this expansion and how the code for the equation would look like afterwards.
If you know another solution than to use a 3D matrix, this would of course also be much appreciated.
If it helps, I need to sum over index i afterwards, so this will reduce the dimensions again afterwards.
To clarrify: i runs from 1 to m and j & l run from 1 to n.

 採用された回答

Matt J
Matt J 2021 年 6 月 2 日
編集済み: Matt J 2021 年 6 月 2 日

0 投票

If it helps, I need to sum over index i afterwards, so this will reduce the dimensions again afterwards.
Don't multiply-then-sum. Do them in one combined step using matrix multiplications (and transpositions, where appropriate).
eq = 2*Y.'*Y - P.'*Y - Y.'*P %entries are eq(j,l)

1 件のコメント

Wilko
Wilko 2021 年 6 月 2 日
Thanks, this solves my issue. I did this earlier in a similar situation, but difference in notation together with some reading about 3D matrices earlier messed up my train of thought.

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

その他の回答 (2 件)

J Chen
J Chen 2021 年 6 月 2 日

0 投票

does mean ? You can calculate the result using three loops on i, j, and l.
David Hill
David Hill 2021 年 6 月 2 日

0 投票

Should be something like this if I understood you correctly.
[m,n]=size(Y);
N=zeros(n);
for j=1:n
for l=1:n
N(j,l)=sum(2*Y(:,j)*Y(:,l)'-Y(:,l)*P(:,j)'-Y(:,j)*P(:,l)','all');
end
end

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

製品

リリース

R2021a

質問済み:

2021 年 6 月 2 日

コメント済み:

2021 年 6 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by