Short function to find min value in vector
2 ビュー (過去 30 日間)
古いコメントを表示
Hi
I have written this simple function:
function minv = findm(vec)
minv= vec(1);
for i = 2:length(vec)
if vec(i) <= minv
minv = vec(i)
end
end
and I feed it with A = [2 4 1] with:
s = findm(A)
But I get the error message "Not enough input arguments". Why is that? I don't see any obvious mistake in the code..
Thanks!
0 件のコメント
採用された回答
José-Luis
2013 年 1 月 15 日
編集済み: José-Luis
2013 年 1 月 15 日
findm() is a built-in Matlab function. Rename your function to something else, e.g. findmin(). Even if you run your function from the folder it is saved in, it is always a good idea to avoid naming your custom functions as Matlab's built-in function. Alternatively, you could use the built-in min() function.
2 件のコメント
José-Luis
2013 年 1 月 15 日
編集済み: José-Luis
2013 年 1 月 15 日
That means that findmin() is not on the path or the current directory, and Matlab can't find it. Try to run the function from the folder it is located in. Alternatively, as Jan says, add the folder where the function is to the path. Then you will be able to run it from everywhere.
その他の回答 (2 件)
Jan
2013 年 1 月 15 日
The M-file must be called "findmin.m", while the actual name of the function does not matter. This M-file must be either in the current directory (see cd command) or better in a user-defined folder in Matlab's path:
addpath('D:\MyMatlabFiles\Experiments')
3 件のコメント
José-Luis
2013 年 1 月 15 日
It means that you are trying to pass a double (use wikipedia for "double precision") to a function, but that there is no function called findmin that accepts double.
I agree that error messages can be cryptic, but I don't think this one is. It is just a matter of getting used to the terminology.
Please accept an answer if it helped you.
Jan
2013 年 1 月 15 日
@MiauMiau: You can define a function in a subfolder like \@cell\XYZ.m or \@double\ABC.m . Functions inside such a folder are called, when the input has the corresponding type (with some further rules if the input has multiple inputs). Now calling XYZ(1) or ABC({1,2,3}) must fail, because the corresponding functions are not defined for this specific class of inputs. The error message "Undefined function XYZ" would be wrong and the exact message is "Undefined function XYZ for input argument of type DOUBLE". You would get more information, when you type: which XYZ -all, namely a list of all functions called XYZ together with the path, such that you can see that it is defined inside a @cell folder.
Image Analyst
2013 年 1 月 15 日
When you call it you have to pass in a vector. You didn't. Let's see the line where you actually called findm(). And tell me whether it was from the command window or another function or script.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Parallel Computing Fundamentals についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!