0

有 2 个矩阵,尺寸为 1440 行 x 241 列。第一个矩阵用其面积填充每个单元格,第二个包含从 0 到 871 的值。这些单元格被分组,值 1 到 871 代表不同的组,即第 1 组由 10 个相邻单元组成,第 2 组由 20 个相邻单元组成,等等

我想构建第三个矩阵,871 行 x 1 列,列出第二个矩阵中每组单元格的面积,通过对第一个矩阵中的相关单元格求和来计算面积。

我尝试运行一个函数,但不断收到此错误:

clear all 
clear
load Clusters_28Aug.mat;  
AR = A>0; 
U = Cluster_Area(AR) 

Cluster_Area 中的错误(第 13 行)i = 1;调用期间未分配输出参数“Clus_Area”(可能还有其他参数)。

我认为变量是在函数中分配的。我怎样才能解决这个问题?

这是我的代码:

function Clus_Area = Cluster_Area(AR)
% Summer 2013 Project
%
% Purpose:  To determine the area of each cluster, by adding up the individual areas of each cell within a cluster.
%  Input
%  AR(i,j) = clusters ID'd, on a 1440 x 241 matrix
%
% Output
% Clus_Area(i,j) = area of each cluster, single column vector, indexed by cluster #

i = 1;
j = 1;

for i = 1:1440;  %For all longitudes        
    for j = 1:120;  %For 30S to Equator, convert 0.25 deg lon to km, varies by latitude         
        if AR > 0;              
            b_1 = (111.41288*cosd(abs((0.25*(j+239))-90)))-(0.0935*cosd(abs(3*((0.25*(j+239))-90))))+(0.00012*cosd(abs(5*((0.25*(j+239))-90))));
            b_2 = (111.41288*cosd(abs((0.25*(j+240))-90)))-(0.0935*cosd(abs(3*((0.25*(j+240))-90))))+(0.00012*cosd(abs(5*((0.25*(j+240))-90))));

            %Use area formula for trapezoid A = 1/2 h (b_1+b_2), where h = 27.8km
            Area_cell = (((0.5)*27.8*1000)*((b_1+b_2)*1000));  %Ans converted to m^2

            %Add up cell areas to get cluster area
            Clus_Area(i,j) = sum(Area_cell);
            disp('Clus_Area')
            %Populate Grid_LAT_LON with area of each cell
            %Grid_LAT_LON(i,j) = Area_cell;         
        end      
    end     

    for j = 121:241;  %For Equator to 30N, convert 0.25 deg lon to km, varies by latitude
        if AR > 0;          
            b_1 = (111.41288*cosd(((0.25*(j+239))-90)))-(0.0935*cosd((3*((0.25*(j+239))-90))))+(0.00012*cosd((5*((0.25*(j+239))-90))));
            b_2 = (111.41288*cosd(((0.25*(j+240))-90)))-(0.0935*cosd((3*((0.25*(j+240))-90))))+(0.00012*cosd((5*((0.25*(j+240))-90))));

            %Use area formula for trapezoid A = 1/2 h (b_1+b_2), where h = 27.8km
            Area_cell = (((0.5)*27.8*1000)*((b_1+b_2)*1000));  %Ans converted to m^2            
            %Add up cell areas to get cluster area
            Clus_Area(i,j) = sum(Area_cell);
            disp('Clus_Area')
            %Populate Grid_LAT_LON with area of each cell
            %Grid_LAT_LON(i,j) = Area_cell;         
        end                       
    end     
end
Z = Clus_Area(i,j);
end
4

1 回答 1

0

你不是想说

if AR(i,j)>0

我不确定当表达式生成数组时 if 语句的语义是什么,但我认为它可能需要表达式的所有元素都为真。

于 2013-07-31T22:42:11.007 回答