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 view (last 30 days)
Show older comments
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.
0 Comments
Answers (2)
Steven Lord
on 1 Oct 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]
B = [5 6; 7 8]
C = A.*B % [1*5, 2*6; 3*7, 4*8]
D = A*B % matrix multiplication
0 Comments
Abhas
on 1 Oct 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:
2 Comments
Steven Lord
on 4 Oct 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.
See Also
Categories
Find more on Operators and Elementary Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!