Effacer les filtres
Effacer les filtres

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

176 vues (au cours des 30 derniers jours)
Patricia  Bracer
Patricia Bracer le 2 Nov 2015
Modifié(e) : Stephen23 le 2 Nov 2015
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

Réponses (1)

Stephen23
Stephen23 le 2 Nov 2015
Modifié(e) : Stephen23 le 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.

Catégories

En savoir plus sur Introduction to Installation and Licensing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by