필터 지우기
필터 지우기

Trouble with function using variable inputs.

조회 수: 2 (최근 30일)
James
James 2014년 1월 17일
댓글: James 2014년 1월 18일
Hello, I am fairly new to Matlab and am having a little difficulty using the "varargin" statement.
I am trying to create a function which multiplies matrices in a specific way.
The number of inputs will always be some multiple of 3 PLUS 1 (i.e. 4,7,10,13...). Taking as an example an input of 4 variables, (a,b,c,d), I want to do the following operation:
M = [1 0;-1/a 1]*[1 b/c;0 1]*[1 0;-1/d 1];
Putting this into a loop, I would have something like:
M = [1 0;-1/a 1];
N = length(varargin); %number of input variables
for k = 2:3:N
b = varargin(k);
c = varargin(k+1);
d = varargin(k+2);
M = M*[1 b/c;0 1]*[1 0;-1/d 1];
end
However, this does not work. I believe it has to do with the fact that the input variables are not doubles. Does anyone have an advice for me?
Please let me know if I was too vague in my description.
Thanks!
  댓글 수: 1
Image Analyst
Image Analyst 2014년 1월 18일
Do you want to do a matrix multiplication or an element-by-element multiplication?

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2014년 1월 18일
편집: Jan 2014년 1월 18일
varargin replies a cell, so you need curly braces:
b = varargin{k};
% The same for b and c
Please do not claim only "this does not work". Showing us the complete error message is much more helpful for solvind your problem. Thanks.
  댓글 수: 1
James
James 2014년 1월 18일
Thanks, Jan! I will make note of that next time I post on here.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by