removing a loop from a script

2 visualizzazioni (ultimi 30 giorni)
Richard
Richard il 15 Nov 2012
I have a matrix 'd' which represents air temperature at specific heights above the ground and for different days of the year. Each column represents the different heights above the ground and each row represent the day:
d = 1+(20-1).*rand(365,5);
The temperature decays exponentially as the height increases and the exponential decrease in air temperature is driven by the exponential coefficient 'kd':
height = 1:5;
kd = 1+(2-1).*rand(365,1);
I can calculate the air temperature by:
newD = zeros(size(d,1),size(d,2));
for i = 1:length(kd);
for ii = 1:length(bthD);
newD(i,ii) = d(i,ii).*exp(-kd(i).*bthD(ii));
end
end
However, I was wondering how this could be done without using a loop?

Risposta accettata

C.J. Harris
C.J. Harris il 15 Nov 2012
Simple:
d = 1+(20-1).*rand(365,5);
bthD = 1:5;
kd = 1+(2-1).*rand(365,1);
newD = d.*exp(-kd*bthD);

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by