我正在Matlab中实现粒子过滤器。如果我尝试对代码进行矢量化处理,则会收到上述错误。但是,如果我不进行向量化,它将很好地工作。我需要对其向量化,以节省for循环所花费的时间。任何帮助,将不胜感激。我已经浏览过其他文章来解决相同的问题,但是无法获得有关我的眼周问题的任何有用信息。
矢量化代码
n = size(X_kmin1,1);
X_k = proc_f(X_kmin1) + mvnrnd(zeros(n,1),proc_Q)';
ykp = meas_h(X_k);
pyx = exp(-0.5 * (yk-ykp)' * ((meas_R)^-1) * (yk-ykp));
W_k = W_kmin1*pyx;
Wt = sum(W_k);
W_k = W_k/Wt;
错误声明
Out of memory. Type "help memory" for your options.
Error in pfFilterStep (line 24)
pyx = exp(-0.5 * ((yk-ykp)' * (meas_R)^-1 * (yk-ykp)));
Error in Test1 (line 32)
[X_k, W_k] = pfFilterStep(X_kmin1, W_kmin1, yk, proc_f, proc_Q, meas_h, meas_R);
工作代码
% Input:
% X_kmin1 [n x N] Particles for state x in time k-1
% W_kmin1 [1 x N] Weights for state x in time k-1
% y_k [m x 1] Measurement vector for time k
% proc_f Handle for process function f(x_k-1)
% proc_Q [n x n] process noise covariance
% meas_h Handle for measurement model function h(x_k)
% meas_R [m x m] measurement noise covariance
%
% Output:
% X_k [n x N] Particles for state x in time k
% W_k [1 x N] Weights for state x in time k
% Your code here!
n = size(X_kmin1,1);
N = size(X_kmin1,2);
for i = 1:N
X_k(:,i) = proc_f(X_kmin1(:,i)) + mvnrnd(zeros(n,1),proc_Q)';
ykp = meas_h(X_k(:,i));
pyx = exp(-0.5 * ((yk-ykp)' * (meas_R)^-1 * (yk-ykp)));
W_k(i) = W_kmin1(i)*pyx;
end
Wt = sum(W_k);
W_k = W_k/Wt;
end