# Sum of Least Squares

6 views (last 30 days)
Robert Demyanovich on 4 Oct 2021
Commented: Matt J on 4 Oct 2021
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)
Mathieu NOE on 4 Oct 2021
hi
you can do in one line .
SumofSquares = sum((Check(:,5) - Check(:,6)).^2);

Siddharth Bhutiya on 4 Oct 2021
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 CommentsShowHide 1 older comment
Matt J on 4 Oct 2021
@Robert Demyanovich But the answer given by @Siddharth Bhutiya is correct with respect to your stated question, so you should Accept-click it.