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!