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 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Steven Lord
2024-10-1
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 个评论
Abhas
2024-10-1
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 个评论
Steven Lord
2024-10-4
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Operators and Elementary Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!