# how to take an alternating range of elements from a vector to make two new vectors

4 views (last 30 days)
Ali Albaidhani on 12 Apr 2022
Commented: Voss on 13 Apr 2022
Hello everyone,
I will try to explain the title with an example.
So let's say i have a vector (a 1x100)
a = [ 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0]
i want to extract only the 1s and the 0s in new vectors. so we will have two 1x50 vectors
a_1=[ 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
a_0=[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
is there a way to do that in matlab?
regards
Ali

Voss on 12 Apr 2022
Edited: Voss on 12 Apr 2022
a = [ 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0];
a_1 = a(a == 1); % elements of a where a equals 1
a_0 = a(a == 0); % elements of a where a equals 0
disp(a_1);
Columns 1 through 37 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 38 through 50 1 1 1 1 1 1 1 1 1 1 1 1 1
disp(a_0);
Columns 1 through 37 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 38 through 50 0 0 0 0 0 0 0 0 0 0 0 0 0
Ali Albaidhani on 13 Apr 2022
thank you so much
Voss on 13 Apr 2022
You're welcome!