Creating a loop for repeated vector values

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

采纳的回答

Voss
Voss 2024-4-25
编辑:Voss 2024-4-25
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 个评论
Voss
Voss 2024-4-25
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);

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Parameterization Methods 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by