【发布时间】:2015-02-20 21:09:54
【问题描述】:
文件lake_powell.dat 包含2000 年至2007 年8 年的水库水位数据。这些数据如表8.9 所示。使用文件中的数据回答下列问题: (a) 确定每年和收集数据的 8 年期间的平均水位高度。 (b) 确定每年有多少个月超过 8 年期间的总体平均值。 (c) 创建一份报告,列出超过总体平均值的每个月份的月份(数字)和年份。例如,六月是第 6 个月。 (d) 确定 8 年期间每个月的平均水位。
原来,我在学习 find 功能时有这个问题。对此,我得到了答案:
load LAKE_POWELL.DAT
% part a
table=[LAKE_POWELL]; % Puts LAKE_POWELL.DAT into a table
yearly=mean(table) % Finds the average elevation of the water level for each year
eight_years=mean(table(:)) % Puts the values of the matrix into one column vector before finding the average the entire matrix
% part b
l=[eight_years] % Puts averages into matrix
i=mean(table) % Finds overall average of data
[y]=(find(table>i)) % Finds which months are above overall average
% part c
fprintf('Month # %f in year %f exceeds overall average',y,column) % Creates a report that lists the month and the year for each of the months that exceed the overall average
% part d
months=table'; % Transposes the table so that the months are now columns
per_month=mean(months) % Determines the average elevation of the water for each month
但现在有人告诉我,我需要使用嵌套循环解决相同的问题。到目前为止,我有:
load LAKE_POWELL.DAT
table=[LAKE_POWELL];
for k=1:length(table)
a=mean(table(k))
eight_year=mean(a)
b=zeros(1,length(table))
for k=1:length(table)
b(k)=find(table>eight_year)
end
end
当然,它不起作用。你有什么建议吗?
【问题讨论】:
-
您不能在两个嵌套的 for 循环中使用相同的索引变量。尝试将第二个“for k”改为使用“j”。
-
@such 我一直报错:在赋值 A(I) = B 中,B 和 I 中的元素个数必须相同。
-
如何调试这样的错误。在 Matlab 中:启用错误停止。运行脚本直到它失败。然后,您将看到它在哪一行失败,以及回溯。然后计算左侧参数的值和右侧参数的值。
标签: matlab