Creating a loop for repeated vector values

Hi,
I am attempting to setup a massive vector made up of Current values for a battery model. Before this piece of code I have included, the PosAccelReqCurrent already has 15 values. Is it possible to make a loop to repeat the values below for x amount of times rather than just copy and pasting it x times? It would make my code a lot of more efficient rather than having thousands of lines of the same code.
As further context, this code is to create many repeated current cycles to test battery degredation over time. Apologies if I have not added enough explanation.
Thank you.
%%%%%%%%%%%%%%%
maxPARC = length(PosAccelReqCurrent);
for x=1:6500
PosAccelReqCurrent(maxPARC+x)=PosAccelReqCurrent(15);
end
for x=1:6500
ActVel(maxPARC+x)=ActVel(15);
end
maxPARC = length(PosAccelReqCurrent);
for x=1:100
PosAccelReqCurrent(maxPARC+x)=0;
end
for x=1:100
ActVel(maxPARC+x)=ActVel(15);
end
maxPARC = length(PosAccelReqCurrent);
for x=1:3470
PosAccelReqCurrent(maxPARC+x)=15;
end
for x=1:3470
ActVel(maxPARC+x)=0;
end
%%%%%%%%%%%%%%%%%%%%

 Akzeptierte Antwort

Voss
Voss am 25 Apr. 2024
Bearbeitet: Voss am 25 Apr. 2024

1 Stimme

No loops necessary.
If those are row vectors, then:
newPARCdata = [PosAccelReqCurrent(15)*ones(1,6500) zeros(1,100) 15*ones(1,3470)];
newAVdata = [ActVel(15)*ones(1,6600) zeros(1,3470)];
PosAccelReqCurrent = [PosAccelReqCurrent repmat(newPARCdata,1,x)];
ActVel = [ActVel repmat(newAVdata,1,x)];
If they are column vectors, then:
newPARCdata = [PosAccelReqCurrent(15)*ones(6500,1); zeros(100,1); 15*ones(3470,1)];
newAVdata = [ActVel(15)*ones(6600,1); zeros(3470,1)];
PosAccelReqCurrent = [PosAccelReqCurrent; repmat(newPARCdata,x,1)];
ActVel = [ActVel; repmat(newAVdata,x,1)];

1 Kommentar

Here's a way that works whether they are row or column vectors:
newPARCdata = [PosAccelReqCurrent(15)*ones(1,6500) zeros(1,100) 15*ones(1,3470)];
newAVdata = [ActVel(15)*ones(1,6600) zeros(1,3470)];
PosAccelReqCurrent(end+(1:numel(newPARCdata)*x)) = repmat(newPARCdata,1,x);
ActVel(end+(1:numel(newAVdata)*x)) = repmat(newAVdata,1,x);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2023b

Gefragt:

am 25 Apr. 2024

Bearbeitet:

am 25 Apr. 2024

Community Treasure Hunt

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

Start Hunting!

Translated by