Why does my code does not work in the if loop

1 Ansicht (letzte 30 Tage)
Low Kar Chuen
Low Kar Chuen am 22 Mär. 2017
Kommentiert: Jan am 22 Mär. 2017
Hi there,
The question ask us to filter out the temperatures that is less than 5 deg C. the output for my T variable is 1 by 539 double matrix. By applying if loop, how do i make temperature that is less than 5 deg C to be 0? The following is my codes:
beta=15; T0=56.2; %intial temperature
t=1:0.0167:10;
T=T0-beta*t;
for n=1:length(T)
if T(n)<5
T=0
end
end
there is an error: Index exceeds matrix dimensions. Please help
  1 Kommentar
Jan
Jan am 22 Mär. 2017
There are no "if-loops". Only for and while create loops.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

ES
ES am 22 Mär. 2017
Bearbeitet: ES am 22 Mär. 2017
Once you set T = 0 inside your if, you lose all the array you created by doing T=T0-beta*t;
so you should have done
if T(n)<5
T(n)=0
end
instead of if, you can also do a
T(T<5) = 0
what this does is, for all values of T<5, it sets it to 0.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by