Unable to perform assignment because the left and right sides have a different number of elements.

I used this code for perfoming kalman filtering for signal data. the measurment are random created.
this massege was appearing (Unable to perform assignment because the left and right sides have a
different number of elements.)
% %Initializations
Pk_prev = [0,0; 0,0];
x_kprev_hat = [0 ; 0];
xk_hat = [];
% Time update equations
A= [0.5 , 0 ; 0 , 0.3];
B = 0;
% Measurement update equations
H = [ 1 , 0];
z = rand(2,200);
E_x=[1,0 ; 0,1]; %Simulating noisy measurements
E_s =[1,0 ; 0,1];
E_y=1
%Measurement model
z_k = H*(z + 1) + E_y;
P = zeros(1,length(z));
% Measurement noise
R =1;%R = 0.000001 for a very low value
% Process noise covariance
Q =[1, 0; 0, 1];
for k = 1: length(z)
x_k_hat_minus = A*x_kprev_hat+ E_x ; %a priori estimate
Pk_minus = A*Pk_prev * A'+ Q; %A priori estimate error covariance
Kk = Pk_minus/(Pk_minus + R);%Kalman gain
x_kprev_hat = x_k_hat_minus + Kk*(z_k(k) - x_k_hat_minus); %A priori estimate
xk_hat = cat(2,xk_hat,x_kprev_hat);%A posteriori estimate
Pk_prev = (1 - Kk)*Pk_minus;%A posterirori estimate error covariance
P(k) = Pk_prev;
end
plot(z_k,'bx-');
hold on;
plot(xk_hat,'gx-');
plot(z*ones(1,200),'rx-');
figure;
plot(P,'r');
E_y =1

2 Comments

Unable to perform assignment because the left and right sides have a
different number of elements.
This was the complete message

Sign in to comment.

 Accepted Answer

The ‘Pk_prev’ variable is a (2x2) matrix. You cannot assign that to a scalar array element in ‘P’.
If you preallocate ‘P’ as:
P = zeros(2,2,length(z));
and then assign it as:
P(:,:,k) = Pk_prev;
your code works. You then need to plot ‘z’ as:
plot(z,'rx-');
Plotting ‘P’ however is not possible with your current code.
You can plot it with this:
figure
hold all
for k = 1:size(P,3)
surf(P(:,:,k))
end
hold off
grid on
view(-30,30)
Experiment to get the result you want.

5 Comments

thanks a lot for your support, I have did what you have suggested as follwoing but another error appeared ,
Unable to perform assignment because the size of the left side is 2-by-2-by-200 and the size
of the right side is 2-by-2.
here is the code
%Process model: x_k = Ax_k-1 + Bu_k + E_x
%Mearuement model:z_k = H(x_k +E_s)+ E_y
%E_x and E_y are process and measurement noise respectively
%Initializations
z = rand(2,200);
P = zeros(2,length(z));
Pk_prev = [1,0; 0,1];
x_kprev_hat = [0,0 ; 0,0];
xk_hat = [];
% Time update equations
A= [0.5 , 0 ; 0 , 0.3];
B = 0;
% Measurement update equations
H = [ 1 , 1];
E_x=[1,0 ; 0,1]; %Simulating noisy measurements
E_s =[1,0 ; 0,1];
E_y=1
%Measurement model
z_k = H*(z + 1) + E_y;
P = zeros(2,2,length(z));
P(:,:,k) = Pk_prev;
% Measurement noise
R =[1, 0; 0, 1];%R = 0.000001 for a very low value
% Process noise covariance
Q =[1, 0; 0, 1];
for k = 1: length(z)
x_k_hat_minus = A*x_kprev_hat+ E_x ; %a priori estimate
Pk_minus = A*Pk_prev * A'+ Q; %A priori estimate error covariance
Kk = (Pk_minus*H')/(H*Pk_minus*H' + R);%Kalman gain
x_kprev_hat = x_k_hat_minus + Kk*(z_k(k) - x_k_hat_minus); %A priori estimate
xk_hat = cat(2,xk_hat,x_kprev_hat);%A posteriori estimate
Pk_prev = (1 - Kk)*Pk_minus;%A posterirori estimate error covariance
P(:,:,k) = Pk_prev;
end
plot(z,'rx-');
hold on;
plot(xk_hat,'gx-');
plot(z,'rx-');
figure;
hold all;
for k = 1:size(P,3);
surf(P(:,:,k));
end
hold off;
grid on;
view(-30,30);
E_y =
1
Unable to perform assignment because the size of the left side is 2-by-2-by-200 and the size
of the right side is 2-by-2.
I do not know what line that error refers to. It would help to know.
You changed your code significantly. It worked before, with the changes I noted. I deleted the first assignment of:
P(:,:,k) = Pk_prev;
that immediately follows your preallocation of ‘P’ because you have not yet defined ‘k’. (The assignment belongs only in the loop.)
That problem corrected, I got different errors, the most significant being ‘matrix dimensions must agree’ with this line:
Kk = (Pk_minus*H')/(H*Pk_minus*H' + R);%Kalman gain
The numerator is a (2x1) vector Pk_minus*H', and in the denominator, you are adding the (2x2) matrix ‘R’ to scalar H*Pk_minus*H'. MATLAB uses ‘automatic singular expansion’ to create the denominator, producing a (2x2) matrix. That still leaves the problem of dividing a (2x1) vector by a (2x2) matrix. That is not possible.
What do you want to do?
thank you very much for your contribution. I have solved this issues as your comments but now the drawings are not as wanted.
%z - voltage to be measured
z = rand(2,800);
%Process model: x_k = Ax_k-1 + E_x
%Mearuement model:z_k = H(x_k+E_s) + E_y
%E_x and E_y are process and measurement noise respectively
%Initializations
Pk_prev = [1 0;
0 1];
x_kprev_hat = [0;
0];
xk_hat = [];
% Time update equations for z from 1-200
A = [0.5 0;
0 0.3];
E_y = 1; % Measurement noise (R)
% Measurement update equations
H = [1 1];
%Simulating noisy measurements
%Measurement model
E_s= [25 ;
25 ];
z_k = H*(z+E_s) + E_y ;
P = zeros(2,2,length(z));
% Process noise covariance
E_x= [1 0;
0 1]; % Q
for k = 1: length(z)
x_k_hat_minus = A*x_kprev_hat+E_x; %a priori estimate
Pk_minus = A*Pk_prev*A' + E_x; %A priori estimate error covariance
Kk = Pk_minus*(H')/(H*Pk_minus*(H') + E_y);%Kalman gain
x_kprev_hat = x_k_hat_minus + Kk*(z_k(k) - H*x_k_hat_minus); %A priori estimate
xk_hat = cat(2,xk_hat,x_kprev_hat);%A posteriori estimate
Pk_prev = (1 - Kk*H)*Pk_minus;%A posterirori estimate error covariance
P(:,:,k) = Pk_prev;
end
plot(z_k,'bx-');
hold on;
plot(xk_hat,'gx-');
plot(z,'rx-');
I cannot follow what you are doing.
I have no suggestions on how to make your code produce the plot you expect.

Sign in to comment.

More Answers (1)

Running your code, it throws an error in this line
P(k) = Pk_prev;
The problem is that PK_prev is of size 2x2 and P is 1x200, then the assignment is incorrect.

Community Treasure Hunt

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

Start Hunting!