How to create a function with a matrix M as an input

184 次查看(过去 30 天)
Hello,
I need to write a function with one input argument: a Matrix M. As an output, it should return a matrix that contains only those elements of M that are in odd rows and columns. I understand the idea but its hard to grasp how i will define a matrix M as an input. What I wrote is
function oddmatrix = odd_index(row,col)
M=rand(row,col); temp=M(1:2:end,1:2:end) oddmatrix=temp(:,:)
end
But then M is not an input anymore.
Any help will be highly appreciated

回答(1 个)

Stephen23
Stephen23 2015-11-2
编辑:Stephen23 2015-11-2
You are told that the function should have one input argument, but then you created a function with two input arguments: row and col. Here is a function with one input argument:
function out = odd_values(M)
... code here
end
Note that you do not need to assign to a temporary variable, you can simply assign to the output variable directly, so that code might look something like this:
out = M(1:2:end,1:2:end);
And now there is nothing else that you need to do except put it together.

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by