Create a vector of n alternating ones and minus ones (★★) - MATLAB Cody - MATLAB Central

Problem 46823. Create a vector of n alternating ones and minus ones (★★)

Difficulty:Rate
Given n, your output should be a vector y of numbers such that the first number is 1 and the numbers following it alternate between -1 and 1. Thus,
if n = 10, then y = [1 -1 1 -1 1 -1 1 -1 1 -1]
if n = 5, then y = [1 -1 1 -1 1]

Solution Stats

62.04% Correct | 37.96% Incorrect
Last Solution submitted on May 10, 2025

Solution Comments

Show comments

Problem Recent Solvers60

Suggested Problems

More from this Author52

Problem Tags

Community Treasure Hunt

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

Start Hunting!