# Indexing sections of arrays

11 views (last 30 days)
Mohammed Kagalwala on 23 Oct 2019
Is there an easy way to extract every section's of n elements of a vector in MATLAB and skip the next n? Say we have
x = linspace(1,10,10); %[1 2 3 4 5 6 7 8 9 10]
I wish to index such that I grab 1,2,3 then skip 4,5,6 and again grab 7,8,9. For example,
a = zeros(1,10); % [0 0 0 0 0 0 0 0 0 0]
a(magic index) = x(magic index)
So now a = [1 2 3 0 0 0 7 8 9 0]
I've only been able to find how to get every nth element but not chunks of n elements.

Jan w on 23 Oct 2019
Define the indices you wish to extract
a = [1,2,3,7,8,9];
x = x(a);
Is this what you mean?
Mohammed Kagalwala on 23 Oct 2019
Not exactly. I don't want a vector [1,2,3,7,8,9]. I want my 'a' vector to be [1 2 3 0 0 0 7 8 9 0]. Jos's answer below works as I wish !

Jos (10584) on 23 Oct 2019
Edited: Jos (10584) on 23 Oct 2019
x = 101:110
n = 3
tf = mod(0:numel(x)-1, 2*n) < n
a = zeros(size(x))
a(tf) = x(tf)
index = find(tf)

#### 1 Comment

Mohammed Kagalwala on 23 Oct 2019
Thank you ! Works perfectly :)