フィルターのクリア

removing a loop from a script

1 回表示 (過去 30 日間)
Richard
Richard 2012 年 11 月 15 日
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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by