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

190 views (last 30 days)
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

Answers (1)

Stephen23
Stephen23 on 2 Nov 2015
Edited: Stephen23 on 2 Nov 2015
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.

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!