判断并将矩阵转化为严格对角占优矩阵

% Judge is a matrix is a strict diagonal dominance matrix
% and Transform the matrix into a strict diagonal dominance matrix
% input:
%   A: the original matrix
% output:
%   X: the strict diagonal dominance matrix that we transform
%   t: the time that the transform process takes

function [X t]= StrictDiagMatrix(A)
tic
%  if the matrix is not a squre matrix,return error
 if size(A, 1) ~= size(A, 2)
        error('It is not a square matrix');
 end
% get the size of the matrix and initial a matrix X to store the output matrix
  n = size(A,1);
  X = zeros(n,n);
% the record array is used to accord if all the row be set
  record = zeros(n);
  for i = 1 : n;
% get the absolute value of every element in every row
    row_max = abs(A(i,:));
% find out the max element of the row,and use pos to accord its position in
    [max_ele,pos] = max(row_max);
% check if the max_ele is the realy max element of the row,if not,return error
    if max_ele <= sum(abs(A(i,:))) - max_ele
        error('The matrix can not be transformed into a strictly diagonally dominant matrix');
    end
% set the row pos of matrix X with A(i,:)
% accord this row has been set
            X(pos,:) = A(i,:);
            record(pos) = 1;
  end
% if there exits any row that has not been set,return error
  if sum(record) ~= n
      error('The matrix can not be transformed into a strictly diagonally dominant matrix');
  end
% output the time it take and output success 
  fprintf('The oringinal matrix input is:\n');
  disp(A);
  fprintf('The time it cost is:   ')
  t = toc;
  disp(t);
  fprintf('It can be transformed to a strictly diagonally dominant matrix: \n');
end

你可能感兴趣的:(数值分析)