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 CenterFile Exchange에서 Whos에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by