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!