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

 채택된 답변

KSSV
KSSV 2017년 3월 31일

0 개 추천

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
KSSV
KSSV 2017년 3월 31일
You see that most of the elements are taking different values...so using a loop will be not handy or simple.

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

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2017년 3월 31일
편집: Andrei Bobrov 2017년 3월 31일

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에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

질문:

2017년 3월 31일

편집:

2017년 3월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by