フィルターのクリア

Vectorize an anonymous function

1 回表示 (過去 30 日間)
AES
AES 2022 年 8 月 11 日
コメント済み: Matt J 2022 年 8 月 11 日
I was wondering whether someone would be able to help/point me in the right direction for vectorizing a anonymous function?
I am trying to fit a multivariate gaussian distribution to data. Here is my code:
for n = 1:length(uniqPosOne)
gaussElpt = @(param) (param(1) + param(2).*(exp(-1/2 .* (uniqPosOne(n, :) - [param(3), param(4)]) * ...
([param(5), param(6); param(6), param(7)]).^(-1) * (uniqPosOne(n, :) - [param(3), param(4)]).')));
a = gaussElpt(startPoint);
tempList(n) = a;
end
I am trying to optimize the parameters that allow for me to fit my data (not shown), I use lsqnonlin.
uniqPosOne is a n x 2 array, params(1) and (2) are offset/gain, params(3) and (4) are vector means, and params(5), (6), (7) are part of covaraince matrix. The params are the parameters used by lsqnonlin to find the parameters with the lowest error.
I want to be able to have a nx2 array (uniqPosOne) and have the function evaluate each pair separately in a vectorized manner, currently I have been unable to do so as attempting vectorization results in a nxn, while I want a nx1.
The main reason is due to time to compute is rather long for a small subset of data. Any help is appreciated.
  3 件のコメント
AES
AES 2022 年 8 月 11 日
Thank you!
Walter Roberson
Walter Roberson 2022 年 8 月 11 日
Or, better yet,
[param(5), param(6); param(6), param(7)] \ (uniqPosOne(n, :) - [param(3), param(4)]).'

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

採用された回答

Matt J
Matt J 2022 年 8 月 11 日
Just replace all the uniqPosOne(n,:) with uniqPosOne:
gaussElpt = @(param) (param(1) + param(2).*(exp(-1/2 .* (uniqPosOne - [param(3), param(4)]) * ...
([param(5), param(6); param(6), param(7)]).^(-1) * (uniqPosOne - [param(3), param(4)]).')));
  5 件のコメント
AES
AES 2022 年 8 月 11 日
Thank you, this is what I was looking to do. If you have time could you breifly explain, or point me towards some resources that may explain, this operation: sum((Sigma\dev).*dev)?
Matt J
Matt J 2022 年 8 月 11 日
Sigma\dev is the more recommended way of doing inv(Sigma)*dev.
See also Walter's comment above.

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

その他の回答 (1 件)

Matt J
Matt J 2022 年 8 月 11 日
編集済み: Matt J 2022 年 8 月 11 日
This FEX submission already appears to do what you want,

カテゴリ

Help Center および File ExchangeLeast Squares についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by