필터 지우기
필터 지우기

How do I concatenate properly?

조회 수: 2 (최근 30일)
Gonzalo
Gonzalo 2022년 9월 8일
댓글: Torsten 2022년 9월 8일
for kk = 1:length(amps)
nStart = round(fs .* tStart(kk))+1; %-- add one to avoid zero index
xNew = shortSinus(amps(kk), freqs(kk), phases(kk), fs, durs(kk));
Lnew = length(xNew);
nStop = Lnew + nStart - 1; %======== Add code
xx(nStart:nStop) = xx(nStart:nStop) + xNew;
%xx(nStart + ((kk - 1)):...
% nStop + ((kk - 1))) ...
% = xx(nStart + ((kk - 1):...
% nStop + ((kk - 1)) + xNew;
end
  댓글 수: 4
Cris LaPierre
Cris LaPierre 2022년 9월 8일
This is not concatenation. This is indexing into and modifying an array. See Ch 5 of MATLAB Onramp for more on how to do this.
Torsten
Torsten 2022년 9월 8일
This is horizontal concatenation of xx with xnew.
a = [1 2 3];
b = [4 5 6];
c1 = [a,b]
c1 = 1×6
1 2 3 4 5 6
c2 = horzcat(a,b)
c2 = 1×6
1 2 3 4 5 6

댓글을 달려면 로그인하십시오.

답변 (1개)

Cris LaPierre
Cris LaPierre 2022년 9월 8일
편집: Cris LaPierre 2022년 9월 8일
You need to put parentheses around your values to the left and right of the colon operators to force the order of operations to calculate a single value on each side.
xx(nStart + ((kk - 1)):nStop + ((kk - 1)))
should be
xx((nStart + kk - 1):(nStop + kk - 1))
Simplifying your example
xx=1:10
xx = 1×10
1 2 3 4 5 6 7 8 9 10
nStart = 2;
nStop = 5;
kk = 1;
xNew = 100;
xx((nStart + kk - 1):(nStop + kk - 1)) = xx((nStart + kk - 1):(nStop + kk - 1)) + xNew
xx = 1×10
1 102 103 104 105 6 7 8 9 10

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by