只返回奇怪的元素 [英] Return only odd elements

查看:240
本文介绍了只返回奇怪的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次尝试学习Matlab,我正在尝试一些由p文件评估的作业。我认为我的功能是正确的,但我一直从评估函数的自动反馈中收到一个负面反馈。


(反馈:参数错误[1 2 3; 4 5 6; 7
8 9])


问题:


编写一个名为odd_index的函数,它将一个矩阵M作为输入参数
并返回一个矩阵,该矩阵仅包含M表示奇数
行和列。换句话说,它将返回M的元素b $ b index(1,1),(1,3),(1,5),...,(3,1),(3,3) ,(3,5),...等
注意,元素的行和列都必须是奇数才能包含在输出中。
以下内容不会被返回:(1,2),(2,1),(2,2),因为行或
列或两者都是偶数。例如,如果M是一个5乘8的矩阵,那么输出必须为
为3×4,因为该函数省略了M的第2行和第4行,并且它也省略了第2,4,6和6列。和8个M。


这是我写的函数:

 函数odd_index 
M = [1:5; 6:10; 11:15; 16:20; 21:25];
M = M(1:2:结束,1:2:结束);
M
end

任何关于我在做什么错误的建议都是赞赏。

解决方案

您的函数需要采用矩阵 M 作为输入参数:

 函数M_out = odd_index(M)
M_out = M(1:2:结束,1 :2:结束);
end

这样,'p文件'可以测试它的各种输入。 / p>

This is my first attempt to learn Matlab and I am trying some homework which is evaluated by a p file. I think my function is correct but I keep getting a negative feedback from the automatic feedback which evaluate the function

(Feedback: Your function made an error for argument(s) [1 2 3;4 5 6;7 8 9])

Problem:

Write a function called odd_index that takes a matrix, M, as input argument and returns a matrix that contains only those elements of M that are in odd rows and columns. In other words, it would return the elements of M at indices (1,1), (1,3), (1,5), ..., (3,1), (3,3), (3,5), ..., etc. Note that both the row and the column of an element must be odd to be included in the output. The following would not be returned: (1,2), (2,1), (2,2) because either the row or the column or both are even. As an example, if M were a 5-by-8 matrix, then the output must be 3-by-4 because the function omits rows 2 and 4 of M and it also omits columns 2, 4, 6, and 8 of M.

This is the function I wrote:

function odd_index
M=[1:5; 6:10; 11:15; 16:20; 21:25];
M=M(1:2:end, 1:2:end);
M
end

Any suggestion about what I am doing wrong here will be appreciated.

解决方案

Your function needs to take in a matrix M as an input argument:

function M_out = odd_index(M)
    M_out = M(1:2:end, 1:2:end);
end

That way, the 'p file' can test it for various inputs.

这篇关于只返回奇怪的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆