Search Program on this blog

Wednesday 19 August 2015

Write a function called separate_by_two that takes a matrix A of positive integers as an input and returns two row vectors. The first one contains all the even elements of A and nothing else, while the second contains all the odd elements of A and nothing else, both arranged according to column-­‐major order of A. You are not allowed to use for-­‐loops or while-­‐loops.

function [u,v] = separate_by_two(A)
%Input A is a matrix of positive integers
%Output u and v are row vectors
%Example: A = [1 2 3;4 5 6;7 8 9]
%         [u,v]=separate_by_two(A)
u=(A(mod(A,2)==0))'; %contain even elements
v=(A(mod(A,2)~=0))'; %contain odd elements

No comments:

Post a Comment