PDA

View Full Version : Write a function called odd_index



CYLONS
جمعه 19 تیر 1394, 16:17 عصر
سلام و خسته نباشید.
صورت سوال:

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.

همونطور که تو صورت سوال ذکر شده تابعی با نام odd_index معرفی بشه که ماتریس ورودی M رو دریافت کنه و در خروجی فقط عناصری از ماتریس رو نشون بده که در سطر و ستون فرد قرار گرفته باشن. "مثلا" برای ماتریس ورودی 8 در 5 زیر، در خروجی فقط درایه های های لایت شده نشون داده بشه.
133116
پ.ن: این تابع باید در مورد ماتریس m*n صدق کنه.
ممنون و متشکر

rahnema1
جمعه 19 تیر 1394, 16:31 عصر
سلام
به این صورت

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

یا خیلی شسته رفته تر:

odd_index = @(M) M(1:2:end, 1:2:end);

CYLONS
جمعه 19 تیر 1394, 17:22 عصر
اولی بهتره. چون متوجه میشم!
ممنون.