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

 采纳的回答

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 个评论

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.

请先登录,再进行评论。

更多回答(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;

类别

在 帮助中心 和 File Exchange 中查找有关 Logical 的更多信息

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!

Translated by