Unable to perform assignment because the size of the left side is 1-by-1

6 views (last 30 days)
I'm writing code to perform a signal simulation (stochastic systems) and it throws me the error "Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 1-by-2." in the code line "L(k,k+i)=L(k,k+i-1)*Phi*(ones(1, k+i)-K(k+i));" of course, I understand that it is a dimensionality problem, I know that the left side is a scalar and the right side a vector of 2 columns. However, I do not know how to solve it, I have tried to modify the dimensions of L(k, k + i) but it does not work for me. I appreciate any help in this regard.
%%%%%Condiciones iniciales
Phi=0.95;
n=50;
P0=1;
x0=sqrt(P0)*randn(1,1);
Q0=0.1;
w0=sqrt(Q0)*randn(1,1);
x(1)=Phi*x0+w0;
%%%%Estado%%%%%%
for k=1:n
Q(k)=0.1;
w(k)=sqrt(Q(k))*randn(1,1);
x(k+1)=Phi*x(k)+w(k);
end
%%%%%Observaciones%%%%%
R0=0.5;
v0=sqrt(R0)*randn(1,1);
z0=x0+v0;
for k=1:n
R(k)=0.5;
v(k)=sqrt(R(k))*randn(1,1);
z(k)=x(k)+v(k);
end
%%%%Filtrado%%%
Cpred0=P0;
K0=Cpred0*[P0+R0]^-1;
fil0=K0*z0;
Cfil0=[1-K0]*Cpred0;
pred(1)=Phi*fil0;
Cpred(1)=Phi^2*Cfil0+Q0;
for k=1:n
K(k)=Cpred(k)*[Cpred(k)+R(k)]^-1;
fil(k)=pred(k)+K(k)*[z(k)-pred(k)];
Cfil(k)=[1-K(k)]*Cpred(k);
pred(k+1)=Phi*fil(k);
Cpred(k+1)=Phi^2*Cfil(k)+Q(k);
end
%%%%%Suavizamiento de punto fijo N=2%%%%%
for k=1:n
L(k,k)=Cfil(k);
pf(k,k)=fil(k);
Cpf(k,k)=Cfil(k);
end
for k=1:n
for i=1:2
kpf(k,k+i)=L(k,k+i-1)*Phi*(Cpred(k+i))^-1;
L(k,k+i)=L(k,k+i-1)*Phi*(ones(1,k+i)-K(k+i));
pf(k,k+i)=pf(k,k+i-1)+kpf(k,k+i)*(z(k+i)-pred(k+i));
Cpf(k,k+i)=Cpf(k,k+i-1)-kpf(k,k+i)*(pred(k+i)+R(k))*kpf(k,k+i);
end
end
Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 1-by-2.
  1 Comment
the cyclist
the cyclist on 31 Jul 2022
Great. (I deleted my comment about the other problem, after I saw you had fixed it.)

Sign in to comment.

Answers (1)

the cyclist
the cyclist on 31 Jul 2022
My best guess as to the problem is that in this expression
ones(1,k+i)-K(k+i)
you don't really intend to add a vector of ones to the scalar K(k+i). Did you just intend to add the scalar 1? If so, you could do
1 + K(k+i)
  3 Comments
César Guillermo Rendón Mayorga
I've done the modification, it seems to work for what I need. I'll continue to work for now. Thank you very much for your help!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!