How to run a function for multiple input matrices?

8 ビュー (過去 30 日間)
jack brown
jack brown 2020 年 4 月 22 日
コメント済み: Geoff Hayes 2020 年 4 月 23 日
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.

回答 (1 件)

Geoff Hayes
Geoff Hayes 2020 年 4 月 22 日
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 件のコメント
jack brown
jack brown 2020 年 4 月 23 日
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 2020 年 4 月 23 日
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);

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by