this 4 matrices r notdiagonally dominant please check if they can be made diagon
ID: 3864334 • Letter: T
Question
this 4 matrices r notdiagonally dominant
please check if they can be made diagonally dominant by swapping the raws
write code on Matlab
Explanation / Answer
function flag = domdiag( A, strOpt )
%
% DOMDIAG Check if a matrix is (strictly) diagonally dominant.
% The input matrix is tested in order to know if its diagonal is
% dominant.
% A matrix has a dominant diagonal if for each row the magnitude of the
% diagonal is greater or equal to the sum of the magnitudes of all the
% other values in that row.
% A matrix has a strcitly dominant diagonal if it has a dominant diagonal
% and at least the magnitude of one diagonal element is greater than the
% sum of the magnitudes of the other elements of that row
%
% Input:
% A - input matrix
% strOpt - 'strict' to check if matrix is strictly diagonally dominant
% otherwise only diagonal dominance is tested
%
% Output:
% flag - indicates if matrix has a dominant diagonal (1) or not (0)
%
% Examples:
% domdiag( [1 4 0 1 1,1 -5 9 20 2 ,15 1 4 5 1,2 2 -5 0 0 ,
0 2 -3 1 -9] )
% returns 0
% domdiag( [9 3 3,12 3 2,1 3 5] )
% returns 0
% domdiag([1 13 2,1 3 9,12 2 -1])
% returns 0
% domdiag([15 -2 4 2 ,0 3 -1 -1,3 3 9 -5,1 1 1 5])
% returns 1
%
% Author: % Version: 1.1
% * H1 is now in the correct position.
% * More efficient (vectorized) implementation.
% * Output is now returned as LOGICAL.
% * Input can now be tested for diagonal dominance or strict
% diagonal dominance.
% Date: 20-03-2017
%
%
%
if nargin == 1
strOpt = ''; % by default check only for diagonal dominance
elseif nargin ~= 2
error('domdiag: invalid input parameters');
end
[ m , n ] = size(A);
if m ~= n
error('domdiag: input matrix must have dimension rows==cols');
end
% magnitude of the diagonal
absDiag = abs(diag(A));
% sum of the magnitude of the elements of each row exept the diagonal
% element
absElem = sum(abs(A), 2) - absDiag;
% check if each row has diagonal magnitude greater or equal to the sum
% of magnitudes of its other values
flag = all(absElem <= absDiag);
% check if each row has diagonal magnitude greater or equal to the sum
% of magnitudes of its other values, and at least the magnitude of one
% diagonal element is greater than the sum of the magnitudes of the
% other elements of that row
if strcmpi(strOpt, 'strict') && flag == true
flag = any(absElem < absDiag);
end
end
Now we will swap the rows to make the matrix diagonally dominant
is_diag_dom_row (mat, i) :=
is(2*abs(mat[i][i]) - lsum(abs(x), x, mat[i]) > 0)$
is_diag_dom (mat) :=
every(lambda([i], is_diag_dom_row (mat, i)),
makelist(i,i,length(mat)))$
swapped_matrix_rows (mat, i1, i2) :=
makelist (
mat[if is(i=i1) then i2 elseif is(i=i2) then i1 else i],
i, makelist(i,i,length(mat)))$
row_swap (mat, i1, i2) := apply(matrix, swapped_matrix_rows(mat, i1, i2))$
domdiag( [1 4 0 1 1,1 -5 9 20 2 ,15 1 4 5 1,2 2 -5 0 0 ,
0 2 -3 1 -9] )
% returns 0
% domdiag( [9 3 3,12 3 2,1 3 5] )
% returns 1
% domdiag([1 13 2,1 3 9,12 2 -1])
% returns 0
MD is diagonally dominant as we check it previously Now after swapping MB is diagonally dominant and MA,MC are not
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.