Borrar filtros
Borrar filtros

How can I apply a function for more than one matrix?

2 visualizaciones (últimos 30 días)
alexp10
alexp10 el 1 de Mayo de 2020
Comentada: Image Analyst el 3 de Mayo de 2020
I'm trying to check if all these matrices A,B,C & D can be made diagonally dominant by swapping rows and if they CAN, store that matrix as M_A , M_B, M_C and M_D. So far it works for A but I have no idea how to do it for all the other matrices and get the result at once. I also can't get the diagonally dominant matrix to be stored as M_A. Any help would be much appreciated!!!
(CODE BELOW)
function [ M, M_A ] = DiagDom
A = [ 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];
B= [ 9 3 3; 12 3 2; 1 3 5];
C= [ 1 13 2; 1 3 9; -12 2 -1];
D= [ 5 -2 4 2; 0 3 -1 -1; 3 3 9 -5;1 1 1 5];
M = {A,B,C,D};
for i=1:numel(M)
count = 0;
while(1) % Perform infinite loop, till you find the diagonally dominant matrix
if itisDiagDom (M{i}) % If this is diagonally dominant, disp and break the loop
disp (['Matrix M is diagonally-dominant']);
celldisp(M) ;
disp([count]);
break;
else
M{i} = M{i}(randperm(size(M{i}, 1)), :);
count= count+1 ;
disp([count]);
if count > 500
M_A= M{1};
M_B= M{2};
M_C= M{3};
M_D= M{4};
disp('M_A is');
disp(M_A);
disp('M_B is');
disp(M_B);
disp('M_C is');
disp(M_C);
disp('M_D is');
disp(M_D);
break ;
end
% Randomly swaps rows
end
end
end
end
function [isdom] = itisDiagDom( A )
isdom = true;
for r = 1:size(A,1)
rowdom = 2 * abs(A(r,r)) > sum(abs(A(r,:)));
isdom = isdom && rowdom;
end
if isdom == 0
disp (['Matrix']);
disp([A]);
disp([' is not diagonally-dominant']);
elseif isdom == 1
disp (['Matrix A is diagonally-dominant']);
disp([A]);
end
end

Respuestas (1)

Ameer Hamza
Ameer Hamza el 2 de Mayo de 2020
This function shows how to save all process and return the output as a cell array
function [ M, M_A ] = DiagDom
A = [ 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];
B= [ 9 3 3; 12 3 2; 1 3 5];
C= [ 1 13 2; 1 3 9; -12 2 -1];
D= [ 5 -2 4 2; 0 3 -1 -1; 3 3 9 -5;1 1 1 5];
M = {A,B,C,D};
M_A = cell(1,numel(M));
for i=1:numel(M)
while(1) % Perform infinite loop, till you find the diagonally dominant matrix
if IsDiagDom (M{i}) % If this is diagonally dominant, disp and break the loop
disp (['Matrix A is diagonally-dominant']);
M_A{i} = IsDiagDom (M{i}) ;
break;
else
M{i} = M{i}(randperm(size(M{i}, 1)), :); % Randomly swaps rows
end
end
end
However, the current loop code gets stuck for matrix B and keeps printing "Matrix A is not diagonally-dominant". You may need to check your algorithm.
  3 comentarios
Ameer Hamza
Ameer Hamza el 3 de Mayo de 2020
It is better to use cell array, then using separate names for the variables. See here why creating different variable names is a bad idea: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval. If you still want to create M_A, M_B, .. then you can do it like this.
[M_A M_B M_C M_D] = M_A{:};
Image Analyst
Image Analyst el 3 de Mayo de 2020
Original question:
"How can I apply a function for more than one matrix?"
I'm trying to check if all these matrices A,B,C & D can be made diagonally dominant by swapping rows and if they CAN, store that matrix as M_A , M_B, M_C and M_D. So far it works for A but I have no idea how to do it for all the other matrices and get the result at once. I also can't get the diagonally dominant matrix to be stored as M_A. Any help would be much appreciated!!!
(CODE BELOW)
function [ M, M_A ] = DiagDom
A = [ 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];
B= [ 9 3 3; 12 3 2; 1 3 5];
C= [ 1 13 2; 1 3 9; -12 2 -1];
D= [ 5 -2 4 2; 0 3 -1 -1; 3 3 9 -5;1 1 1 5];
M = {A,B,C,D};
for i=1:numel(M)
count = 0;
while(1) % Perform infinite loop, till you find the diagonally dominant matrix
if itisDiagDom (M{i}) % If this is diagonally dominant, disp and break the loop
disp (['Matrix M is diagonally-dominant']);
celldisp(M) ;
disp([count]);
break;
else
M{i} = M{i}(randperm(size(M{i}, 1)), :);
count= count+1 ;
disp([count]);
if count > 500
M_A= M{1};
M_B= M{2};
M_C= M{3};
M_D= M{4};
disp('M_A is');
disp(M_A);
disp('M_B is');
disp(M_B);
disp('M_C is');
disp(M_C);
disp('M_D is');
disp(M_D);
break ;
end
% Randomly swaps rows
end
end
end
end
function [isdom] = itisDiagDom( A )
isdom = true;
for r = 1:size(A,1)
rowdom = 2 * abs(A(r,r)) > sum(abs(A(r,:)));
isdom = isdom && rowdom;
end
if isdom == 0
disp (['Matrix']);
disp([A]);
disp([' is not diagonally-dominant']);
elseif isdom == 1
disp (['Matrix A is diagonally-dominant']);
disp([A]);
end
end

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by