Need different constants for ranges in matrix

1 visualizzazione (ultimi 30 giorni)
Noah Varner
Noah Varner il 6 Dic 2022
Risposto: Kartik il 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?

Risposte (1)

Kartik
Kartik il 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'.

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by