MATLAB Answers

0

How to access n vector elements, then skip n elements, then access the following n elements and so on?

Asked by Michal Knapek on 14 Jun 2018 at 15:11
Latest activity Edited by Michal Knapek on 14 Jun 2018 at 22:33

I would like to access vector elements like this:

a_new = [a(1:10) a(21:30) a(41:50) ... ]

Is there any elegant way how to do that without loops?

The vector is huge, so something like

k=[];
n=10;
for i=1:floor(length(a)/n)
  if mod(i,2)==0
    kk=((i-1)*n):((i)*n);
    k=[k kk];
  end;
end;
a_new=a(k);

cannot be used.

Thanks, Michal

  0 Comments

Sign in to comment.

1 Answer

Answer by Stephen Cobeldick on 14 Jun 2018 at 15:18
Edited by Stephen Cobeldick on 14 Jun 2018 at 15:59
 Accepted Answer

Assuming that the number of elements of your vector A is exactly divisible by 20:

M = reshape(A,20,[])
V = M(1:10,:)
V = reshape(V,1,[])

If required, pad/trim the vector A to make it a suitable length.


Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today