MATLAB Answers

0

How to split array in n part every m points ?

Asked by Charles Herman on 15 Mar 2019
Latest activity Edited by Matt J
on 18 Mar 2019
Hi everyone,
This question is very frustrating. 3 months ago, I found, 'by chance', a function that I didn't need, and now I need it and I don't remenber the name...
For approximately 4 hours i searched this function, and it seems I don't have 'luck' anymore
On a matrix of 1*n, this function split this matrix into vectors of p values with a step of r .
For example :
A = [ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ];
B = function(A,p,r) ; returned B = [ 1 2 3 ; 2 3 4 ; 3 4 5 ; 4 5 6 ; ... ; ... ;17 18 19 ; 18 19 20 ]
assuming p = 3 and r = 1
Does anyone know the name of this function ?
There was no iteration, or loop, it was just a simple function but very powerfull for a specified purpose : - )
Thank you in advance to help me to get my lucky charm back

  0 Comments

Sign in to comment.

Products


Release

R2018a

1 Answer

Answer by Matt J
on 15 Mar 2019
Edited by Matt J
on 15 Mar 2019
 Accepted Answer

I don't know of any built-in function that will do that, but it is easy to implement:
function B=foo(A,p,r)
N=numel(A);
idx=(0:r:N-p).'+(1:p);
B=A(idx);
end

  3 Comments

" I don't know of any built-in function that will do that, but it is easy to implement: "
Maybe I dreamt :-/ but I was pretty sure that it exists.
Nevertheless, I'll try your code and "yes!" it's a deal :)
Mine was based on a 'for loop' and it took 0.059481 seconds while yours only took 0.005267 seconds.
In the few days I will probably find this secret function one more time, in that case I'll leave it as a comment.
Thank you a lot for your help
You're welcome, but please Accept-click the answer if it addresses your question.
Oupsi : )
Now it's done
Have a nice day

Sign in to comment.