Sum of Least Squares

조회 수: 6 (최근 30일)
Robert Demyanovich
Robert Demyanovich 2021년 10월 4일
답변: John 2023년 7월 31일
I have a table, Check, where I would like to subtract column 6 from column 5 (to obtain a residual) and then square the residual. Then for all of the rows I would like to sum the squares of the residuals. Since MATLAB is supposed to be able to handle working with matrices and arrays much better than say visual basic, it seems I should be able to do this with one or two lines of codes. Maybe something like:
Check(:,7) = (Check(:,5) - Check(:,6))^2
SumofSquares = sum(Check(:,7)
  댓글 수: 1
Mathieu NOE
Mathieu NOE 2021년 10월 4일
hi
you can do in one line .
SumofSquares = sum((Check(:,5) - Check(:,6)).^2);

댓글을 달려면 로그인하십시오.

채택된 답변

Siddharth Bhutiya
Siddharth Bhutiya 2021년 10월 4일
You would have to use curly braces {} indexing in order to do this. Using () will give you a table and operations like subtraction, addition, etc are not defined for tables. Using {} would give you the contents of the table, which could then be used to do what you want.
>> Check = array2table(magic(6));
>> Check{:,7} = (Check{:,5} - Check{:,6}).^2;
>> SumOfSquares = sum(Check{:,7})
SumOfSquares =
156
  댓글 수: 2
Robert Demyanovich
Robert Demyanovich 2021년 10월 4일
Mathieu NOE's answer works. So it wasn't a table but a matrix (I guess)
Matt J
Matt J 2021년 10월 4일
@Robert Demyanovich But the answer given by @Siddharth Bhutiya is correct with respect to your stated question, so you should Accept-click it.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

John
John 2023년 7월 31일
xi = [1491214 17];
yi = [-3.97, 5.18, 20.43, 29.58, 35.68, 44.83];
n = length(xi);
A = [n sum(xi); sum(xi) sum(xi.^2)];
C = [sum(yi); sum(xi.*yi)];
Z = A\C;
a =Z(1);
b = Z(2);
fprintf ('y = %.5f + %.5f x \n',a,b)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by