有2个矩阵,尺寸1440行乘241列。第一个矩阵用其区域填充每个单元格,第二个矩阵包含从0到871的值。这些单元格被分组,值1到871表示不同的组,即组1由10个相邻单元组成,组2由20个相邻单元组成,等
我想构建第三个矩阵,871行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
答案 0 :(得分:0)
你不想说
if AR(i,j)>0
当表达式导致数组时,我不确定if语句的语义是什么,但我认为它可能要求表达式的所有元素都为真。