Need different constants for ranges in matrix

1 view (last 30 days)
Noah Varner
Noah Varner on 6 Dec 2022
Answered: Kartik on 20 Mar 2023
speed=zeros(12,10080);
cases=zeros(12,10080);
B=zeros(12,10080);
for n=1:12
for r=1:9
if t(r,n)<=0
t(r,n)=1;
elseif t(r,n)>0
for i=1:10080
if i==t(r,n)
B(n,i)=c(r,n);
cases(n,i)=B(n,i);
speed(n,i)=m(r,n);
else
B(n,i)=B(n,i);
end
end
end
end
end
I want to somehow change the value for speed within certain ranges. There are only about 5 values that the speed could be, and I dont want it to change for every iteration. How would I set it to change whenever t=i ? and how do I get it to remain that value until another iteration of t equals i?

Answers (1)

Kartik
Kartik on 20 Mar 2023
Hi,
You can set a temporary variable 'temp_speed' to hold the value of 'm(r,n)' for the current iteration of the 'for' loop and then use that value to fill the speed matrix whenever 't(r,n) == i'. Here's an example of how you can modify the code:
speed=zeros(12,10080);
cases=zeros(12,10080);
B=zeros(12,10080);
for n=1:12
for r=1:9
if t(r,n)<=0
t(r,n)=1;
elseif t(r,n)>0
temp_speed = m(r,n);
for i=1:10080
if i==t(r,n)
B(n,i)=c(r,n);
cases(n,i)=B(n,i);
speed(n,i)=temp_speed;
elseif i > t(r,n) && i <= t(r,n) + 5 % Example range of values
speed(n,i) = temp_speed; % Set the value to the temporary variable
else
B(n,i)=B(n,i);
end
end
end
end
end
In this example, I've added an 'elseif' condition to check if the value of 'i' is within a certain range and then set the value of 'speed' to 'temp_speed' within that range. If the value of 'i; is not within that range, it will retain its current value in 'speed'. The value of 'temp_speed' is set outside the 'for' loop and only changes when 't(r,n) == i'.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!