Separate even from odd numbers in a vector - with a loop - MATLAB Cody - MATLAB Central

Problem 44484. Separate even from odd numbers in a vector - with a loop

Difficulty:Rate

Using a loop, rearrange a vector of integers such that the odd numbers appear at the beginning, and even numbers at the end. The order within each group should be preserved.

Examples:

Input:  v = [1, 0, 2, 9, 3, 8, 8, 4]
Output: w = [1, 9, 3, 0, 2, 8, 8, 4]
Input:  v = [2
             7
             0
             3
             2]
Output: w = [7
             3
             2
             0
             2]
Input:  v = []
Output: w = []

Solution Stats

28.35% Correct | 71.65% Incorrect
Last Solution submitted on Mar 10, 2025

Problem Comments

Solution Comments

Show comments

Problem Recent Solvers450

Suggested Problems

More from this Author25

Problem Tags

Community Treasure Hunt

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

Start Hunting!
Go to top of page