フィルターのクリア

How to change the MIN function?

1 回表示 (過去 30 日間)
Cristian
Cristian 2014 年 5 月 9 日
コメント済み: Geoff Hayes 2014 年 5 月 12 日
I wrote a function:
function MIN = mymin(vec)
MIN = vec(1);
for i1 = 2:length(vec)
if MIN > vec(i1), MIN = vec(i1);
end
end
end
I need to satisfy the condition: The function is not receiving a vector; rather, all the values are separate arguments.
How to do it?

採用された回答

Geoff Hayes
Geoff Hayes 2014 年 5 月 9 日
編集済み: Geoff Hayes 2014 年 5 月 9 日
Sounds like there are a variable number of inputs to this function. So your function can be used like:
mina = mymin(1,2,3,4,5); % five inputs
minb = mymin(4.5,9,3); % three inputs
If that is the case, then you want to use the varargin (variable arguments in) input variable in your function signature:
function minval = mymin(varargin)
% varargin is a cell array
The number of arguments in, nargin, is something you can use in your function body to indicate how many inputs have been passed to your function. Try the above and see what happens.
  2 件のコメント
Cristian
Cristian 2014 年 5 月 9 日
This correctly?
function M=mymin(varargin)
M=varargin{1};
for i1 = 2:numel(varargin)
if M > varargin{i1}
M = varargin{i1};
end
end
end
Geoff Hayes
Geoff Hayes 2014 年 5 月 12 日
Yes, that looks to he correct. Try using nargin too.

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by