How to perform mathematical operation of arrays inside loop?
    조회 수: 2 (최근 30일)
  
       이전 댓글 표시
    
I know it is a very basic question, but I have not been able to solve it. I have an array X(28*1). Using values of X for the first time, I would calculate E=B-A*X where E=(352*1), A(352*28) and B=(352*1). Then if the sum of E is larger than 10^-40, The follwoing operations are done:
C=diag(E*E');
W=diag(C)^-1;
X=(A'*W*A)^-1*A'*W*B
Then the updated value of X will be used for E=B-A*X operation. I understand whole thing is required to be inside a loop. But I can not find the exact process to write the code. If anyone has any idea, It would be much appriciated. I have attached the X for 1st iteration, A, and B as .mat file.
댓글 수: 6
  Bruno Luong
      
      
 2023년 7월 19일
				If you want to do robust linear fit with outliers, I recommend you do L1 fitting
minimize norm(A*X - B, 1)
rather than weighted 2-norm (more robust than l2, but still not very robust). 
This requires linear programing linprog of optimization toolbox.
답변 (3개)
  Matt J
      
      
 2023년 7월 18일
        
      편집: Matt J
      
      
 2023년 7월 18일
  
      for i=1:N
    E=B-A*X;
    if norm(E,1)<1e-40, continue; end
    C=E.^2;
    X=lscov(A,B,1./C);
end
댓글 수: 3
  Matt J
      
      
 2023년 7월 20일
				
      편집: Matt J
      
      
 2023년 7월 20일
  
			@ANANTA BIJOY BHADRA I notice that the second column of your A matrix is zero, which iof course makes A ill-conditioned.
A=load('A').A_primary_A;
[l,h]=bounds(A(:,2))
cond(A)
cond(A(:,  [1,3:end]))  %skip second column
  Bruno Luong
      
      
 2023년 7월 20일
				
      편집: Bruno Luong
      
      
 2023년 7월 20일
  
			Good catch. But I beleive the solution of "\" or lscov, both based on QR decomposition would not be affected by 0 column and remain accurate, despite the warning. The solution corresponds to 0-column is simply nil.
A=rand(10,3);
b=rand(10,1);
As=A; As(:,end+1)=0;
w = 0.1+rand(size(b));
format long
A\b
As\b
lscov(A,b,w)
lscov(As,b,w)
  Mrutyunjaya Hiremath
      
 2023년 7월 18일
        E = B-A*X;
while(sum(E(:))> 10^-40)
    C=diag(E*E');
    W=diag(C)^-1;
    X=(A'*W*A)^-1*A'*W*B;
end
댓글 수: 0
  Voss
      
      
 2023년 7월 18일
        E = B-A*X;
while sum(E,'all') > 1e-40
    C = diag(E*E');
    W = diag(C)^-1;
    X = (A'*W*A)^-1*A'*W*B;
    E = B-A*X;
end
댓글 수: 0
참고 항목
카테고리
				Help Center 및 File Exchange에서 Linear Programming and Mixed-Integer Linear Programming에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




