removing a loop from a script

2 views (last 30 days)
Richard
Richard on 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?

Accepted Answer

C.J. Harris
C.J. Harris on 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);

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!