I am getting errors that I am using ".*" and "*" wrongly in my matrix multiplications in my code. I am poor at matrix algebra and don't know when to use each operator.

1 visualización (últimos 30 días)
I am getting errors that I am using ".*" and "*" wrongly in my matrix multiplications in my code. I am poor at matrix algebra and don't know when to use each operator.
clear;
clc;
H=[5, -5;
2.5, -2.5;
0, -2.5;
0, -4;
-4, -2.5]
diagW=diag([1, 1, 1, 1, 1]*10^4);
Z=[0.85;
0.8;
0.6;
0.4;
-0.81]
transH=H';
%Ht=transpose of H;
HtWH=transH*diagW.*H;
HtWZ=transH*diagW.*Z;
X=(inv(HtWH)).*HtWZ;
Zhat=Z.*X;
%Estimation of errors.
ESTofErrors=Z-Zhat;
%Value of the cost function
variance=[0.015, 0.01, 0.005, 0.015, 0.01]
CF=0;
for i=1:5
CF = CF + ((Zhat(i))^2/(variance(i))^2)
end
alpha=0.01;
K=3;
Xka=chi2cdf(0.1, 3);
Xka=11.345;
G=HtWH;
invG=inv(G);
HinvGHt=H.*invG.*Ht;
R=ESTofErrors;
S=R-(H*invG.*transH);
diagS=diag(S);
%Expected value of the cost function.
EVCF=sum(diagS,2);
%Calculating the standardized error
SE=(variance)/sqrt(5);
Errors: Arrays have incompatible sizes for this operation.

Respuestas (2)

Steven Lord
Steven Lord el 1 de Oct. de 2024
Read through this documentation page and ask yourself the question: do I want to multiple each element of one array by the corresponding element of the other array, or do you want to perform matrix multiplication? In the former case use the .* operator and in the latter case use the * operator.
A = [1 2; 3 4]
A = 2×2
1 2 3 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
B = [5 6; 7 8]
B = 2×2
5 6 7 8
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
C = A.*B % [1*5, 2*6; 3*7, 4*8]
C = 2×2
5 12 21 32
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
D = A*B % matrix multiplication
D = 2×2
19 22 43 50
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Abhas
Abhas el 1 de Oct. de 2024
Hi William,
It looks like you're encountering issues with using element-wise operations (.*) and matrix operations (*) in MATLAB. Here's a brief explanation of when to use each operator:
  • *: This is used for matrix multiplication. Use this when you want to perform a dot product or multiply matrices according to the rules of linear algebra.
  • .*: This is used for element-wise multiplication. Use this when you want to multiply corresponding elements of two matrices or vectors of the same size.
Here's your corrected MATLAB code that fixes the error:
clear;
clc;
H = [5, -5;
2.5, -2.5;
0, -2.5;
0, -4;
-4, -2.5];
diagW = diag([1, 1, 1, 1, 1] * 10^4);
Z = [0.85;
0.8;
0.6;
0.4;
-0.81];
transH = H';
% Correct matrix multiplication
HtWH = transH * diagW * H; % Use * for matrix multiplication
HtWZ = transH * diagW * Z; % Use * for matrix multiplication
% Solve for X using matrix division instead of inverse
X = HtWH \ HtWZ;
% Element-wise multiplication for estimation
Zhat = H * X;
% Estimation of errors
ESTofErrors = Z - Zhat;
% Value of the cost function
variance = [0.015, 0.01, 0.005, 0.015, 0.01];
CF = 0;
for i = 1:5
CF = CF + ((Zhat(i))^2 / (variance(i))^2);
end
alpha = 0.01;
K = 3;
Xka = chi2cdf(0.1, 3);
Xka = 11.345;
G = HtWH;
invG = inv(G);
% Correct matrix multiplication
HinvGHt = H * invG * transH;
R = ESTofErrors;
% Correct subtraction and multiplication
S = R - (H * invG * transH * R);
diagS = diag(S);
% Expected value of the cost function
EVCF = sum(diagS, 2);
% Calculating the standardized error
SE = variance / sqrt(5);
You may refer to the below MathWorks Documentation links to have a better understanding on matrix multiplication and element wise operations:
  1. https://www.mathworks.com/help/matlab/ref/mtimes.html
  2. https://www.mathworks.com/help/matlab/matlab_prog/array-vs-matrix-operations.html
  2 comentarios
Steven Lord
Steven Lord el 4 de Oct. de 2024
Set an error breakpoint to enter debug mode when an error occurs, then run your code. When MATLAB enters debug mode, look at the line of your code where it's stopped and use whos or the Workspace browser to see the sizes of the variables used on that line.

Iniciar sesión para comentar.

Categorías

Más información sobre Operators and Elementary Operations en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by