To fill the value from the variable until the Num counts

2 ビュー (過去 30 日間)
Smithy
Smithy 2022 年 9 月 14 日
コメント済み: Smithy 2022 年 9 月 14 日
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

採用された回答

Chunru
Chunru 2022 年 9 月 14 日
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 件のコメント
Smithy
Smithy 2022 年 9 月 14 日
Wow..thank you very much for your huge helps. It works really well.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by