How to add matrices in a function with varying input arguments

5 ビュー (過去 30 日間)
Hail
Hail 2014 年 7 月 16 日
コメント済み: Hail 2014 年 7 月 16 日
I have a function: myfunction(varargin), where the varargin are matrices. I want this function to add all of the matrices I input. Hows the best way of doing this?

採用された回答

James Tursa
James Tursa 2014 年 7 月 16 日
Do you mean simply adding up all of the varargin individual inputs, like this?
if( nargin )
matrix_sum = varargin{1};
for k=2:nargin
matrix_sum = matrix_sum + varargin{k};
end
end

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 7 月 16 日
You can use one cell array containing all your matrices
  3 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 7 月 16 日
M1=[1 2;3 4] % first matrix
M2=[1 2 3;0 1 0]% second matrix
M={M1,M2}
Use M as cell array, where
M{1} %is the first matrix
M{2} % the second matrix
Hail
Hail 2014 年 7 月 16 日
Sorry I dont think I was clear. I want to create a function that no matter how many matrices I input, it will add them together. All of the matrices will have the same dimensions.
So if I had: M1=[1 2 3; 4 5 6] M2=[2 3 4; 5 6 7]
myfunction(M1,M2)
it'll out put
[3 5 7;9 11 13]

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

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by