Add a number every n values

13 visualizzazioni (ultimi 30 giorni)
Nikolas Spiliopoulos
Nikolas Spiliopoulos il 10 Dic 2018
Risposto: Stephen23 il 10 Dic 2018
Hi all,
I have a question:
If I have an array
A=[ 1 2 3 4 5 3 0 2 8 3 2 1 0 3 2 0 0 1 1 0 3 1 5 3 2 8 1 0 3 2];
Is it possible to put the a value "a=800" after every 6 elements of A; (or after every 3 elements etc.)
So I get
b=[ 1 2 3 4 5 a 3 0 2 8 3 a 2 1 0 3 2 a 0 0 1 1 0 a 3 1 5 3 2 a 8 1 0 3 2 a];
thanks a lot!!
Nikolas

Risposta accettata

madhan ravi
madhan ravi il 10 Dic 2018
A=[ 1 2 3 4 5 3 0 2 8 3 2 1 0 3 2 0 0 1 1 0 3 1 5 3 2 8 1 0 3 2];
a=4;
after=6;
b=nan(1,numel(A)+after);
b(after:after:end)=a;
b(isnan(b))=A

Più risposte (1)

Stephen23
Stephen23 il 10 Dic 2018
>> B = reshape(A,3,[]);
>> B(4,:) = 800;
>> B = reshape(B,1,[])
B =
1 2 3 800 4 5 3 800 0 2 8 800 3 2 1 800 0 3 2 800 0 0 1 800 1 0 3 800 1 5 3 800 2 8 1 800 0 3 2 800

Categorie

Scopri di più su Multidimensional Arrays in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by