1

How to select the N elements then ignore the next N ones and then select the next N ones and so on?

foe example of the array a = 1:100;

b = 1,2,3,7,8,9,13,14,15,...

Divakar
  • 218,885
  • 19
  • 262
  • 358
user3482383
  • 295
  • 3
  • 11

1 Answers1

1

Approach # 1

a1 = reshape([a zeros(1,2*N - rem(numel(a),2*N))],N,[]);
out = reshape(a1(:,1:2:end),1,[]);
if rem(numel(a),2*N)<N
    out = out(1:N*floor(numel(a)/(2*N)) + rem(numel(a),2*N)); %//output
end

Approach # 2

a1 = vec2mat(a,N)'; %//'
out = reshape(a1(:,1:2:end,:),1,[]);
if rem(numel(a),2*N)<N
    out = out(1:N*floor(numel(a)/(2*N)) + rem(numel(a),2*N)); %//output
end

Approach # 3

mat1 = [true false];
mat2 = reshape(mat1(ones(N,1),:),[],1);
mat3 = reshape(mat2(:,ones(1,ceil(numel(a)/(2*N)))),[],1);
out = a(mat3(1:numel(a)));  %//output
Divakar
  • 218,885
  • 19
  • 262
  • 358
  • Dear Divakar, thank you, i have to look it up to understand how it works, how about selecting from for example an array, anything, for example an array of odd numbers? – user3482383 Mar 31 '14 at 19:11
  • @user3482383 You just have to change `N` for that – Luis Mendo Mar 31 '14 at 19:20
  • @user3482383 For that `N` has to be `2`. Also, check out the edits, for comments to help you understand, at least for the method 2. – Divakar Mar 31 '14 at 19:21
  • how about if i just want to have the m group of them? for example in the above example the first 3 ones, like 1,2,3,7,8,9,13,14,15 ?! – user3482383 Mar 31 '14 at 19:27
  • @user3482383 Number of groups as `m`? `count_Ngroups` is what is `m` then, if I understood you correctly. – Divakar Mar 31 '14 at 19:29
  • @Divakar hi, i asked this question three month ago, is there any faster way to do this? – user3482383 Jul 15 '14 at 13:46
  • @user3482383 Check out the edits. Even if all of these edited approaches fail to deliver your expected efficiency in terms of runtime, could you report back their performances? – Divakar Jul 15 '14 at 15:58