How to add an extra for loop

1 次查看(过去 30 天)
I have a time series data which is 46723-by -1. I want to populate another matrix with some specific time series. this is the code i have.
newTime = zeros(); % the new array with 4 columns, each column to have a specified time series
for j = 1:length(time)
if time(j) >=7 && time(j) <= 11
newTime(j,1) = time(j) ;
elseif time(j) >=56 && time(j) <= 59
newTime(j,2) = time(j) ;
elseif time(j) >=99 && time(j) <= 106
newTime(j,3) = time(j) ;
elseif time(j) >=133 && time(j) <= 143
newTime(j,4) = time(j) ;
end
end
Now the above code works fine, however, as you can see the columns of the newTimes has (1,2,3,4) which i want to do in a loop, is there a way to do this?

采纳的回答

KSSV
KSSV 2020-9-26
newTime = cell(1,4); % cell because, we are not sure of dimensions in each case
R = [7 11 ; 56 59 ; 99 106 ; 133 143 ] ; % the four case ranges
for i = 1:4
newTime{i} = time(time >=R(i,1) && time <= R(i,2)) ;
end
  2 个评论
suleiman abdullahi
suleiman abdullahi 2020-9-26
Thanks for the answer, however, this generates the follwoing error.
Operands to the || and && operators must be convertible to logical scalar values.
Error in Day2 (line 72)
newTime{i} = time(time >=R(i,1) && time <= R(i,2)) ;
suleiman abdullahi
suleiman abdullahi 2020-9-26
Thanks i fixed the problem, we supposed to use only one & instead of &&.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by