How do I concatenate properly?
Afficher commentaires plus anciens
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 commentaires
Gonzalo
le 8 Sep 2022
Torsten
le 8 Sep 2022
Concatenation would be
xx = [xx,xnew]
I don't know exactly what you try do do with the xx array.
Cris LaPierre
le 8 Sep 2022
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.
This is horizontal concatenation of xx with xnew.
a = [1 2 3];
b = [4 5 6];
c1 = [a,b]
c2 = horzcat(a,b)
Réponses (1)
Cris LaPierre
le 8 Sep 2022
Modifié(e) : Cris LaPierre
le 8 Sep 2022
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
nStart = 2;
nStop = 5;
kk = 1;
xNew = 100;
xx((nStart + kk - 1):(nStop + kk - 1)) = xx((nStart + kk - 1):(nStop + kk - 1)) + xNew
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!