removing a loop from a script

1 次查看(过去 30 天)
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?

采纳的回答

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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by