removing a loop from a script

1 visualización (últimos 30 días)
Richard
Richard el 15 de Nov. de 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?

Respuesta aceptada

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

Más respuestas (0)

Categorías

Más información sobre Numeric Types en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by