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

조회 수: 176 (최근 30일)
Patricia  Bracer
Patricia Bracer 2015년 11월 2일
편집: Stephen23 2015년 11월 2일
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에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by