Need different constants for ranges in matrix

조회 수: 1 (최근 30일)
Noah Varner
Noah Varner 2022년 12월 6일
답변: Kartik 2023년 3월 20일
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?
  댓글 수: 1
Noah Varner
Noah Varner 2022년 12월 6일
m(r,n) contains the different speeds that it should be.

댓글을 달려면 로그인하십시오.

답변 (1개)

Kartik
Kartik 2023년 3월 20일
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'.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by