Return only odd elements

FeliceM

This is my first attempt to learn Matlab and I am trying some homework which is evaluated by a p file. I think my function is correct but I keep getting a negative feedback from the automatic feedback which evaluate the function

(Feedback: Your function made an error for argument(s) [1 2 3;4 5 6;7 8 9])

Problem:

Write a function called odd_index that takes a matrix, M, as input argument and returns a matrix that contains only those elements of M that are in odd rows and columns. In other words, it would return the elements of M at indices (1,1), (1,3), (1,5), ..., (3,1), (3,3), (3,5), ..., etc. Note that both the row and the column of an element must be odd to be included in the output. The following would not be returned: (1,2), (2,1), (2,2) because either the row or the column or both are even. As an example, if M were a 5-by-8 matrix, then the output must be 3-by-4 because the function omits rows 2 and 4 of M and it also omits columns 2, 4, 6, and 8 of M.

This is the function I wrote:

function odd_index
M=[1:5; 6:10; 11:15; 16:20; 21:25];
M=M(1:2:end, 1:2:end);
M
end

Any suggestion about what I am doing wrong here will be appreciated.

MattG

Your function needs to take in a matrix M as an input argument:

function M_out = odd_index(M)
    M_out = M(1:2:end, 1:2:end);
end

That way, the 'p file' can test it for various inputs.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Return a list, which contains a pair of elements, but only if the respective elements' sums are odd

toggleClass() only working on odd elements

Sort only the odd elements of an array

How to return a new list that contains just odd elements from data using only recursion

numpy linspace -- keep only odd elements

I want to return only the odd numbers in a list

BeautifulSoup tag.children only gets odd-numbered elements

Iterating over an odd (even) elements only in a range-based loop

How to return a new array only containing odd numbers

only return records with odd count in a table with sql command

How to use an arrow function to return an array with all its even elements incremented by 1, and odd elements decremented by 1?

Nested array filter with return only unique elements

Return elements that appear only once in array

PHP for each Loop return only certain elements

Mongodb Return only matched elements within array

Only return selected GeoJSON elements in Leaflet

Return a list that only includes duplicate elements

Return only elements with maximum number of ocurrences

Odd behavior of function return

Odd return syntax statement

how can I output return only odd friends name from this array in JavaScript

Using odd index to return odd numbers JavaScript

Printing Odd and Even Elements of an Array

css odd and even on nested elements

Extract elements of list at odd positions

Extract elements of list at odd positions

Remove odd elements from ArrayList

Reduction of odd number of elements CUDA

Odd spacing between SVG elements