필터 지우기
필터 지우기

Add a number every n values

조회 수: 10 (최근 30일)
Nikolas Spiliopoulos
Nikolas Spiliopoulos 2018년 12월 10일
답변: Stephen23 2018년 12월 10일
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