Clear Filters
Clear Filters

isolate the odd and even integers in the matrix.

6 views (last 30 days)
Hi, we were tasked to isolate odd and even integers in a matrix given by the user. I tried doing it but it keeps on getting an error at line 3. Here's what I've done so far:
function [B,C1,C2] = problem2
A = input('Enter Values: ');
B = rem(A,2);
if B == 0
disp('even')
isolate(B == 0);
else
disp('odd')
isolate (B ~= 0);
end
The command window shows:
>> problem2()
Enter Values: 4 8 3 6 5 10 7 14 (This is the number I entered to test the program)
Error using problem2 (line 3)
Error: Unexpected MATLAB expression.

Answers (1)

madhan ravi
madhan ravi on 8 Sep 2019
Edited: madhan ravi on 8 Sep 2019
function [B,Even,Odd] = problem2
A = input('Enter Values: '); % input must be [4,2,6,3,5] for example , don't forget the [ ]
B = rem(A,2);
Even=A(B == 0);
Odd=A(B ~= 0);
end
  2 Comments
CaiX
CaiX on 8 Sep 2019
thank you for the response :) However, is it possible to display the even and odd integers afterwards? I tried doing it but the if function doesn't work:
function [B,evenvector,oddvector] = problem2
A = input('Enter Values: ');
B = rem(A,2);
evenvector = A(B == 0);
oddvector = A(B ~= 0);
if B == 0
disp('evenvector')
else
disp('oddvector')
end
I would like to achieve this kind of output:
problem2()
Enter Values: [4 8 3 6 5 10 7 14]
evenvector:
4 8 6 10 14
oddvector:
3 5 7
madhan ravi
madhan ravi on 8 Sep 2019
[B,T] = problem2
function [B,T] = problem2
A = input('Enter Values: ');
B = rem(A,2);
T=table;
T.evenvector = A(B == 0);
T.oddvector = A(B ~= 0);
end

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!