Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you make every other integer in an array equal to 0 in matlab?

Tags:

arrays

matlab

Lets say I have an array

Y = [1, 2, 3, 4, 5, 6]

I want to make a new array that replaces every other number with 0, so it creates

y = [1, 0, 3, 0, 5, 0]

How would I go about approaching this and writing code for this in a efficient way?

like image 848
user2093864 Avatar asked Feb 17 '23 11:02

user2093864


1 Answers

This should do that:

Y(2:2:end) = 0;

With this line you basically say each element starting from the seconds up to the last, in steps of two, should be zero. This can be done for larger steps too:, Y(N:N:end) = 0 makes every Nth element equal to 0.

like image 121
ThijsW Avatar answered Mar 02 '23 00:03

ThijsW