shift values in an array systematically

2 views (last 30 days)
Hi
I have an array of say 1 x 320. Now after each multiple of 40, I want to include 7 times a number 11, and then continue with the next 40 entries of the original array and so on. As an illustration: array(1:40) would be as before, array(41:47) would contain 11s now, and array(48:67) would contain what was previously in array(41:60), and so on, for all multiples of 40. How can I do that most efficiently? Thanks

Accepted Answer

Jos (10584)
Jos (10584) on 3 Dec 2017
Here is one way:
x = 1:320 ;
dp = 40 ;
v11 = repmat(11,1,7) ;
c = arrayfun(@(k) [x(k:min(k+39,end)) v11], 1:dp:numel(x), 'un',0) ;
out = cat(2, c{:})

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!