how to seperate even or odd elements and make row vectors from a matrix
6 views (last 30 days)
Show older comments
Muhammad Usman Saleem
on 28 May 2015
Commented: Mohazam Awan
on 22 Mar 2017
Hi everyone I am going to attempt that question : 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.
I am using that code
function[odd_ones even_ones]=separate_by_two(A)
odd_fin = rem(A,2) ~= 0;
number_odd = A(odd_fin);
number_even = A(~odd_fin);
odd_ones=number_odd';
even_ones=number_even';
end
but getting that error
Problem 7 (separate_by_two):
Feedback: Your program made an error for argument(s) 1
What correction i needed in my code? thanks in advance for assistance..
0 Comments
Accepted Answer
Roger Stafford
on 28 May 2015
The only error I see is that your returned values are in reversed order. According to the statement of the problem you should have
function[even_ones, odd_ones] = separate_by_two(A)
1 Comment
Mohazam Awan
on 22 Mar 2017
This function gives only odd numbers as output. mine one gives even ones
More Answers (2)
charu sharma
on 20 Aug 2015
Your order of output is not correct as they asked even ones in first output. You can refer this for different solution of same ques: http://farzicoders.blogspot.in/2015/08/write-function-called-separatebytwo.html
0 Comments
Mohazam Awan
on 22 Mar 2017
function [ even,odd ] = result( A ) %UNTITLED Summary of this function goes here % Detailed explanation goes here [r,c]=size(A); x=1; y=1; for i=1:r for j=1:c if rem(A(i,j),2)==0 even(x)=A(i,j); x=x+1; else if rem(A(i,j),2)~=0; odd(x)=A(i,j); y=y+1; end end end
end end
0 Comments
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!