create a function that takes a matrix input, that gives output matrix of odd elements

I'm new to Matlab, and trying to learn the basics. I am having a hard time creating a function that allows for any matrix as an input, in which you get an output matrix that is of only the odd elements. I don't have an issue with getting the odd elements from an input matrix, but rather have an issue setting up the function for an input of any matrix values of interest. Could someone give me an example of what this would look like?

 Accepted Answer

Have a file called oddelements.m (or whatever name.m you pick), and in that file you would have this:
% oddelements: (insert a 1-line description of the function here)
function Y = oddelements(x)
% insert your code to get the odd elements of X and put the result in Y
end
Have this function file somewhere on the MATLAB path. Then you can call it as follows:
Y = oddelements(X); % Or B = oddelements(A), etc.
What are the "odd elements" of a matrix? The odd values? Or the odd positions? Or what?

1 Comment

Thanks for the response. I want to call all elements in the odd rows and columns. So if you have a 5x8 matrix, you end up with elements from (1,1), (1,3), and so on in a 3x4 matrix. Only the odd positions.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!