Not enough input arguments error

Hello. I'm trying to write a function that receives a matrix and then finds the mininum value in each column and then returns a vector of column minimums. The code is below. I'm getting not enough input error and I'm not sure what is wrong with the code. I'd greatly appreciate it if someone could help me.
function answer = matlabbooktest(matrix)
[rows, columns] = size(matrix);
answer=zeros(1,columns);
vector=zeros(1,columns);
for i=1:columns
for j=1:rows
vector(1,j)=matrix(j,i);
end
answer(1,i)=min(vector);
end
disp(answer)

6 件のコメント

dpb
dpb 2018 年 11 月 24 日
We'd have to see how you called it to know, but it would appear if that's the message you tried to reference the function matlabbooktest without an argument.
As aside, is it prescribed you you solve the problem? You can do the requested operation in one line if you're allowed to use builtin min -- if you're supposed to find the minima by not using the builtin function, then you didn't do that...would be interesting here to see the actual question wording.
John Tucker
John Tucker 2018 年 11 月 24 日
編集済み: John Tucker 2018 年 11 月 24 日
I didn't call the function. When I run it, it just gives that error. How is it without argument? Isn't matrix the input argument?
Edit: nothing was prescribed. I'm just learning matlab on my own using a book called MATLAB: a practical introduction to programming and problem solving. This question is one of the practice exercises in the book
Elias Gule
Elias Gule 2018 年 11 月 24 日
I guess you ran it by clicking on the run button... that's equivalent to calling the function. Now as you click the run button - a particular function gets executed (that function is called a 'callback' in Matlab lingo); it is that function that actually calls your function; and since your function requires an input which that button's callback is not supplying (of course), that is why you're getting an error.
dpb
dpb 2018 年 11 月 24 日
"Just running" a function is the same as calling it -- it must have an input argument; matrix is a dummy variable that is only known inside the function -- it gains a value only by association with the actual argument passed when the function is called.
John Tucker
John Tucker 2018 年 11 月 24 日
Gotcha. I also fixed the code. Also realized that min function also works. Code is below if you wanted to see it. Thanks very much for the help!
function answer = matlabbooktest(matrix)
[rows, columns] = size(matrix);
answer=zeros(1,columns);
vector=zeros(1,columns);
for i=1:columns
for j=1:rows
vector(1,j)=matrix(j,i);
end
answer(1,i)=min(vector(1:rows));
end
disp(answer)
end
dpb
dpb 2018 年 11 月 25 日
But you don't need any other function unless the requirement is something more than just returning the row vector of column minima--
>> m=randi(10,4) % a sample dataset
ans =
3 1 4 1
3 5 9 5
10 9 3 6
9 5 6 6
>> min(ans) % min function does column minima by default
ans =
3 1 3 1
>> min(m,[],2) % to get row minima instead...
ans =
1
3
3
5
>>
Hence my confusion over the purpose of writing another function for the particular exercise.
One can return the locations as well with the optional additional return value. For column minima, it's the row (first occurrence in case of ties) or column if row minima are returned. The other location is inferred, of course.

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

回答 (0 件)

カテゴリ

質問済み:

2018 年 11 月 24 日

コメント済み:

dpb
2018 年 11 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by