How can I implement the 3*3 matrix in a for loop?

 Accepted Answer

N = 20 ;
n = 3 ;
x = linspace(0,1) ;
y = linspace(0,1) ;
z = linspace(0,1) ;
%LHS
A = zeros(3,3) ;
A(1,1) = sum(x.^2) ;
A(2,1) = sum(x.*y) ;
A(3,1) = sum(x) ;
A(1,2) = A(2,1) ;
A(2,2) = sum(y.^2) ;
A(3,2) = sum(y) ;
A(1,3) = A(3,1) ;
A(2,3) = A(3,2) ;
A(3,3) = n ;
%RHS
B = zeros(3,1) ;
B(1) = sum(x.*z) ;
B(2) = sum(y.*z) ;
B(3) = sum(z) ;

2 Comments

Thanks but can I do this in a for loop?Just to make the code a bit general
You see that most of the elements are taking different values...so using a loop will be not handy or simple.

Sign in to comment.

More Answers (1)

x = (1:3)';
y = (2:4)';
z = (6:8)';
n = 5;
m = numel(x);
a = [x,y,ones(m,1)];
lhs = a.'*a;
lhs(end) = n;
rhs = a.'*z;
abc = lhs\rhs;

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

No tags entered yet.

Asked:

on 31 Mar 2017

Edited:

on 31 Mar 2017

Community Treasure Hunt

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

Start Hunting!