Add a number every n values

11 次查看(过去 30 天)
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

采纳的回答

madhan ravi
madhan ravi 2018-12-10
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

更多回答(1 个)

Stephen23
Stephen23 2018-12-10
>> 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

类别

Help CenterFile Exchange 中查找有关 Multidimensional Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by