how to vectorize squared function
2 ビュー (過去 30 日間)
古いコメントを表示
Hi I have a problem considering calculating a squared function of a grid
so I have 2 vectors X=rand(N,D) and Y=rand(M,D)
I want to generate a function that returs a matrix s.t.
for i = 1:N
for j = 1:M
r2(i,j) = (X(i,:)-Y(j,:))*L*(X(i,:)-Y(j,:))';
end
end
is there a way to vectorize this cleverly?
It needs to run in Simulink (code generation)
0 件のコメント
回答 (2 件)
Bruno Luong
2021 年 7 月 22 日
編集済み: Bruno Luong
2021 年 7 月 22 日
X=rand(3,10);
Y=rand(4,10);
L=rand(10);
N=size(X,1);
M=size(Y,1);
XX=reshape(X,N,1,[]);
YY=reshape(Y,1,M,[]);
YX=reshape(XX-YY,N*M,[]);
r2=reshape(sum(YX.'.*(L*YX')),[N M])
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!