How to run a function for multiple input matrices?

8 visualizzazioni (ultimi 30 giorni)
jack brown
jack brown il 22 Apr 2020
Commentato: Geoff Hayes il 23 Apr 2020
Hello,
Below is a code for swapping rows in a matrix to make it into diagonally dominant form.
A = [2 7 1 1 2; -1 -3 2 -23 2; 13 1 3 4 1; 1 2 -6 0 1; 1 -1 -2 3 -11];
B = [1 10 3; 2 1 8; 15 -5 3]
function makeDD(A)
%I originally put A = [2 7 1 1 2; -1 -3 2 -23 2; 13 1 3 4 1; 1 2 -6 0 1; 1 -1 -2 3 -11] here
% but I would like to make it work % for multiple matrices.
% test to see if a valid permutation exists
[maxrow,maxind] = max(abs(A),[],2);
if all(maxrow > (sum(abs(A),2) - maxrow))
A(maxind,:) = A;
end
DD_A = A
end
How can I make it work so that it can have multiple inputs or is it easiest to just have 2 functions one after the other and if so how can i make it so that they work in succession?
And how do i store the answer matrices with the names DD_A and DD_B?
Thanks in advance for any help, I been trying for ages to figure this out.

Risposte (1)

Geoff Hayes
Geoff Hayes il 22 Apr 2020
jack - just call the function twice and store the output in separate variables or in a cell array. You will need to change your function signature so that it returns a matrix
function [DD] = makeDD(A)
% I originally put A = [2 7 1 1 2; -1 -3 2 -23 2; 13 1 3 4 1; 1 2 -6 0 1; 1 -1 -2 3 -11] here
% but I would like to make it work % for multiple matrices.
% test to see if a valid permutation exists
[maxrow,maxind] = max(abs(A),[],2);
if all(maxrow > (sum(abs(A),2) - maxrow))
A(maxind,:) = A;
end
DD = A;
Then in the command line (or from another script) just do
A = [2 7 1 1 2; -1 -3 2 -23 2; 13 1 3 4 1; 1 2 -6 0 1; 1 -1 -2 3 -11];
B = [1 10 3; 2 1 8; 15 -5 3];
C = cell(2,1);
C{1} = makeDD(A);
C{2} = makeDD(B);
  2 Commenti
jack brown
jack brown il 23 Apr 2020
I still cannot get it to work
Command window:
A = [2 7 1 1 2; -1 -3 2 -23 2; 13 1 3 4 1; 1 2 -6 0 1; 1 -1 -2 3 -11];
B = [1 10 3; 2 1 8; 15 -5 3];
C = [1 13 2; 1 3 9; -12 2 -1];
D = cell(3,1);
C{1} = makeDD(A);
C{2} = makeDD(B);
C{3} = makeDD(C);
function [DD] = makeDD(A)
% test to see if a valid permutation exists
[maxrow,maxind] = max(abs(A),[],2);
if all(maxrow > (sum(abs(A),2) - maxrow))
A(maxind,:) = A;
end
DD = A;
I need the answers to be in the form DD_A= [...] DD_B=[...] etc.
What is it that i'm doing wrong? (This is my first year doing matlab which is probably obvious)
Geoff Hayes
Geoff Hayes il 23 Apr 2020
jack - from your code
D = cell(3,1);
C{1} = makeDD(A);
C{2} = makeDD(B);
C{3} = makeDD(C);
D is the cell array, so you should be doing
D = cell(3,1);
D{1} = makeDD(A);
D{2} = makeDD(B);
D{3} = makeDD(C);
However, if you really need to have separate variables for each with names like DD_, then just do
DD_A = makeDD(A);
DD_B = makeDD(B);
DD_C = makeDD(C);

Accedi per commentare.

Prodotti


Release

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by