There is a nasty way of making this array. I'm saying it's nasty because it's hard to read what is going on, please put a lot of comments around this bit of code if you use it!
You want to create the set of indices 1, 2, 3, 3, 4, 5, 5, 6, 7, 7, ...
, repeating the odd ones (except 1, and also make sure the last one is not repeated). The sequence of repeated odd values is really the floor or the sequence 1, 2/3, 4/3, 6/3, 8/3, ...
, which is easy to generate: 1:2/3:N
. This creates the sequence 1, 1, 2, 3, 3, ...
, we need to remove the first element.
It turns out (by experimenting) that the index N
doesn't get repeated if N
is odd (as desired) but gets skipped if N
is even. So we generate the index sequence up to N+1
, and remove the last element. This way, N
is not repeated if odd, but is always there.
So we have:
g = [5; 6; 11; 12; 17; 23; 24; 29; 30]; % input data
indx = floor(1:2/3:numel(g)+1); % indices with odd elements repeated
indx = indx(2:end-1); % remove first and last element
result = g(indx); % index