Effacer les filtres
Effacer les filtres

isolate the odd and even integers in the matrix.

6 vues (au cours des 30 derniers jours)
CaiX
CaiX le 8 Sep 2019
Commenté : madhan ravi le 8 Sep 2019
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.

Réponses (1)

madhan ravi
madhan ravi le 8 Sep 2019
Modifié(e) : madhan ravi le 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 commentaires
CaiX
CaiX le 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 le 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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by