Effacer les filtres
Effacer les filtres

Come compilare un array tornando all'inizio quando sono alla fine dell'array

3 vues (au cours des 30 derniers jours)
Giulia Della Moretta
Giulia Della Moretta le 16 Sep 2023
Buonasera, nel mio programma matlab per la tesi di laurea vado a compilare dei vettori in base a ciò che accade nell'intervallo temporale (24 time step, quindi lunghezza vettori =24). Si verificano delle certe condizioni che mi condizionano anche i 3 intervalli successivi rispetto al momento in cui accadono.
Se tale condizione si verifica alla fine dell'array, quindi nei time-step, 22,23 o 24, devo far in modo di sovrascrivere i valori iniziali dell'array che ho creato in precedenza (quindi alla posizione 1,2,3 eccetera in base all'istante in cui mi trovo, sempre considerando KK+3). Gli array che devo compilare sono 4 e sono G,B,SoC, Curt.
Vi ringrazio se qualcuno mi può aiutare.
for i=kk:(kk+3)
if (kk+3)<length(Pcar)
G(i)=0
B(i)=B(ist_batt)-Pcar(i)
Curt(i)=0
SoC=B(i)/Cap*100
elseif (kk+1)>length(Pcar)
Devo dire di riempire la posizione dei vettori n.24,1,2,3,
che significa quindi dover ritornare a sovrascriver l'inizio dell'array
elseif (kk+2)>length(Pcar)
devo inserire i valori in 23,24,1,2
elseif (kk+3)>length(Pcar)
devo inserire i valori in 22,23,24,1
end
end

Réponses (1)

Dheeraj
Dheeraj le 25 Sep 2023
Hi,
I understand you are trying to populate a vector with elements depending upon previous elements if satisfied a condition you can modify your code this way so you could achieve the circular consistency.
% Initialize arrays
G = zeros(1, 24);
B = zeros(1, 24);
SoC = zeros(1, 24);
Curt = zeros(1, 24);
% Loop through your time steps
for t = 1:24
% Your code to compute G, B, SoC, and Curt at the current time step
% Check if the condition occurs at the end of the time interval
if t >= 22
% Update the arrays accordingly, considering the 3rd consecutive element
G(t - 21) = ... % Update G
B(t - 21) = ... % Update B
SoC(t - 21) = ... % Update SoC
Curt(t - 21) = ... % Update Curt
end
end
% Continue with your calculations or analysis
Hope this helps!

Community Treasure Hunt

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

Start Hunting!