To fill the value from the variable until the Num counts
조회 수: 2 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
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)
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Whos에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!