Add a number every n values

11 vues (au cours des 30 derniers jours)
Nikolas Spiliopoulos
Nikolas Spiliopoulos le 10 Déc 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

Réponse acceptée

madhan ravi
madhan ravi le 10 Déc 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

Plus de réponses (1)

Stephen23
Stephen23 le 10 Déc 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

Catégories

En savoir plus sur Multidimensional Arrays dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by