Beginner needs help with basic function

5 ビュー (過去 30 日間)
Charlie Harris
Charlie Harris 2016 年 8 月 19 日
コメント済み: Sarah Heady 2017 年 2 月 22 日
The Assignment:
. Write a function called even_index that takes a matrix, M, as input argument and returns a matrix that contains only those elements of M that are in even rows and columns. In other words, it would return the elements of M at indices (2,2), (2,4), (2,6), ..., (4,2), (4,4), (4,6), ..., etc. Note that both the row and the column of an element must be even to be included in the output. The following would not be returned: (1,1), (2,1), (1,2) because either the row or the column or both are odd. As an example, if M were a 5-by-8 matrix, then the output must be 2-by-4 because the function omits rows 1, 3 and 5 of M and it also omits columns 1, 3, 5, and 7 of M.
My Function:
function even_index(M)
M(2:2:end,2:2:end)
The Problem?
If I go to command window and do M = [1 2 3;4 5 6] and create a matrix, and then run "even_index(M)" I get the correct answer (5). However, when i use the required "grader " program, It says
Problem 2 (even_index):
Testing with argument(s) [1 2 3;4 5 6]
Feedback: Your program made an error for argument(s) [1 2 3;4 5 6]
Your solution is _not_ correct
What am I missing?
  2 件のコメント
Sarah Heady
Sarah Heady 2017 年 2 月 22 日
Hey Charlie did you ever figure this out? I have this code: function M_out = even_index(M) M_out = M(2:2:end, 2:2:end); end -- but it always gives me this error: Not enough input arguments. --What did you do to fix this error?
Sarah Heady
Sarah Heady 2017 年 2 月 22 日
Never mind. I was running the wrong thing.

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

採用された回答

Walter Roberson
Walter Roberson 2016 年 8 月 19 日
Your function does not return anything, it displays the results instead. Functions that return something have an "=" on the function line.
  1 件のコメント
Charlie Harris
Charlie Harris 2016 年 8 月 19 日
Thank You, I wondered about that? I will keep trying.

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

その他の回答 (0 件)

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by