How to add matrices in a function with varying input arguments
5 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
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
0 个评论
更多回答(1 个)
Azzi Abdelmalek
2014-7-16
You can use one cell array containing all your matrices
3 个评论
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Multidimensional Arrays 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!