Effacer les filtres
Effacer les filtres

To fill the value from the variable until the Num counts

1 vue (au cours des 30 derniers jours)
Smithy
Smithy le 14 Sep 2022
Commenté : Smithy le 14 Sep 2022
Hello everybody,
I would like to make the variable as 'time2' from the 'time' variable.
It fills the value from the time variable until the num counts.
In the case, length(time) is 8 and to make the 30 length of varaible of time as time2,
it will be time(end) + time(end) + time(end) + time....
For it I used several elseif function.
If the num is 100, I need to make more elseif...
Is there a way to make more general for this ?? .....
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
time = [5.10616303,9.433357995,13.76055296,18.08774792,23.19391095,29.37822416,35.56253737,40.66870040];
T = length(time);
num = 30;
time2 = zeros(num,1);
for i=1:num
if i <= T
time2(i) = time(i);
elseif i > T && i <= 2*T
time2(i) = time(end) + time(i-T);
elseif i > 2*T && i <= 3*T
time2(i) = time(end) + time(end) + time(i-(2*T));
elseif i > 3*T && i <= 4*T
time2(i) = time(end) + time(end) + time(end) + time(i-(3*T));
end
end

Réponse acceptée

Chunru
Chunru le 14 Sep 2022
time = [5.10616303,9.433357995,13.76055296,18.08774792,23.19391095,29.37822416,35.56253737,40.66870040];
T = length(time);
num = 30;
time2 = zeros(num,1);
time2(1:T) = time;
nseg = ceil(num/T); % number of segment of length T
resttime2 = time(:) + (1:nseg-1)*time(end);
resttime2 = resttime2(:);
time2(T+1:end) = resttime2(1:(num-T));
% display the results
reshape(time2, 6, 5)
ans = 6×5
5.1062 35.5625 63.8626 95.0980 127.1123 9.4334 40.6687 70.0469 99.4251 131.4395 13.7606 45.7749 76.2312 104.5313 135.7667 18.0877 50.1021 81.3374 110.7156 140.0938 23.1939 54.4293 86.4436 116.8999 145.2000 29.3782 58.7564 90.7708 122.0061 151.3843
  1 commentaire
Smithy
Smithy le 14 Sep 2022
Wow..thank you very much for your huge helps. It works really well.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Environment and Settings dans Help Center et File Exchange

Tags

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by