Pixel size iteration for loop

3 ビュー (過去 30 日間)
Dude
Dude 2016 年 10 月 2 日
コメント済み: Walter Roberson 2016 年 10 月 3 日
I'm trying to calculate the area of a pixel (1 deg x 1 deg) on a spherical figure (earth) based on the equation
Aring = 2πR^2|sin(lat1) sin(lat2)| (R=6371 km^2)
for latitudes between -90 and 90. I think I need to implement some sort of for loop such as below. I think my lack of knowledge on the use of for loops is causing me error when implementing this code. I'm trying to output a matrix that has the area for a ring around the earth between every degree of latitude (180x360). Can anyone help point me in the right direction?
for ii = -90:90
AreaArray(ii,:) = 2*pi*(6371^2)*abs(sin(ii*pi/180)-sin((ii-1)*pi/180))
end

回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 10 月 2 日
There is nothing obviously wrong with that loop.
If you have not initialized AreaArray before you start the for loop, then because the right hand side is a scalar, you would end up creating a column vector of values.
If you initialize AreaArray to have multiple columns, then each iteration of the for loop will set all of those columns for a row to be copies of the same value, as many copies as you have columns. As the formula appears to be independent of longitude that should be fine.
  2 件のコメント
Dude
Dude 2016 年 10 月 3 日
編集済み: Dude 2016 年 10 月 3 日
Suppose I do this by adding AreaArray above the for loop. When I run this, I receive the error "Subscript indices must either be real positive integers or logicals." Any idea why? Sorry for the elementary question.
for AreaArray = [-90:1:90]
ii = -90:90
AreaArray(ii,:) = 2*pi*(6371^2)*abs(sin(ii*pi/180)-sin((ii-1)*pi/180))
end
Walter Roberson
Walter Roberson 2016 年 10 月 3 日
iivals = -90:90;
for idx = 1 : length(iivals)
ii = iivals(idx);
AreaArray(idx,:) = 2*pi*(6371^2)*abs(sin(ii*pi/180)-sin((ii-1)*pi/180));
end

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by