How to run a function for multiple input matrices?
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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.
0 Kommentare
Antworten (1)
Geoff Hayes
am 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 Kommentare
Geoff Hayes
am 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);
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!