MATLAB Answers

0

Create a new vector with even numbers first.

Asked by Michael Noak on 20 Oct 2019 at 18:18
Latest activity Answered by ABHILASH SINGH on 20 Oct 2019 at 19:15
I have a vector = [20 21 22 23 24 25 26 27 28 29]
My task is to create a new vector from the first, with the even numbers first and then the odd numbers = [20 22 24 26 28 21 23 25 26 27 28 29]
Last I have to make it the Dame way but Vice versa.

  0 Comments

Sign in to comment.

Tags

Products


Release

R2019a

1 Answer

Answer by ABHILASH SINGH on 20 Oct 2019 at 19:15

x = [20 21 22 23 24 25 26 27 28 29]
% Find indices where x is even:
evenIndices = rem(x, 2) == 0
% Extract only the even numbers into a new vector.
allTheEvenNumbers = x(evenIndices)

  0 Comments

Sign in to comment.