shift values in an array systematically

2 Ansichten (letzte 30 Tage)
Pegah Kassraian Fard
Pegah Kassraian Fard am 3 Dez. 2017
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

Akzeptierte Antwort

Jos (10584)
Jos (10584) am 3 Dez. 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{:})

Weitere Antworten (0)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by