What is the meaning of this line?

3 views (last 30 days)
Hello
I want to know why we are using the line
sumX2=sumX2+(listOfPoints(1,i)-Xc(1,:))^2;
Why 2 times sumX2, and what is the purpose of using 2 times, and can anyone let me know?
What are the changes in the result ?
sumX2=0; sumY2 =0; sumZ2=0; sumXY=0; sumYZ=0; sumXZ=0;
for i=1:N
sumX2 = sumX2 + (listOfPoints(1,i)-Xc(1,:))^2;
sumY2 = sumY2 + (listOfPoints(2,i)-Xc(2,:))^2;
sumZ2 = sumZ2 + (listOfPoints(3,i)-Xc(3,:))^2;
sumXY = sumXY + (listOfPoints(1,i)-Xc(1,:))*(listOfPoints(2,i)-Xc(2,:));
sumYZ = sumYZ + (listOfPoints(2,i)-Xc(2,:))*(listOfPoints(3,i)-Xc(3,:));
sumXZ = sumXZ + (listOfPoints(1,i)-Xc(1,:))*(listOfPoints(3,i)-Xc(3,:));
end

Accepted Answer

KALYAN ACHARJYA
KALYAN ACHARJYA on 3 Dec 2019
Edited: KALYAN ACHARJYA on 3 Dec 2019
"why 2 times sumX2 and what is the purpose of using 2times and can anyone do let me know?"
sumX2=sumX2+...
Here sumX2 updated during entire iteration
Let say example
sumX2=1;
for i=1:4;
sumX2=sumX2+i;
end
#
In first Iteration, ehen i=1
sumX2=sumX2+i=1+1=2
In 2nd Iteration, ehen i=2, sumX2=2
sumX2=sumX2+i=2+2=4
...so on..
In each ireration sumX2 used the last updated values, for current sumX2 calculation
Hope it helps!
  5 Comments
KALYAN ACHARJYA
KALYAN ACHARJYA on 5 Dec 2019
Great!
Keep Learning Vimal
Walter Roberson
Walter Roberson on 5 Dec 2019
But i am getting only one value in this loop as respective row.
The expression (1/N)* sum(listOfPoints(1,:)) would sum the row vector listOfPoints(1,:) resulting in a scalar, and then would divide the scalar by N, resulting in a scalar. You then assign the result to Xc(1,:) which would assign the same scalar to all columns that exist in the first row of Xc. If, for example, Xc already existed with 17 columns, then all 17 columns of the first row will be assigned the exact same mean value.
The code I suggested,
Xc = mean(listOfPoints, 2);
is a little different in that it will result in Xc being exactly one column wide, no matter how big the existing Xc was. If you really need Xc to keep the same number of columns and have each column be exactly the same within each row, then there are a number of different was to achieve that, including,
Xc = mean(listOfPoints, 2) * ones(1, size(Xc,2));

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!