回复: 如何在特殊数组中求取均值
lz 仅说说个人意见 如果不使用一些复杂的数据结构 我想能否这样解决这个问题
建立几个1维数组 height[] number[] sum_col_2[] sum_col_3[] 及对应的平均数组avg_col_2[] avg_col_3[]
height[]用于统计曾经出现过的高度 number[]记录height出现的次数 sum_col_2[]和sum_col_3[]分别记录第二列和第三列的数据和 avg_col_2[]和avg_col_3[]分别记录第二列和第三列的平均数
从处理上可以分为以下几个部分:
Pseudo:
% initialization:suppose height[],number[],sum_col_2[],sum_col_3[],avg_col_2[] and avg_col_3[] are all zero, the element number of which are all n
% suppose there are n rows in the 3-d matrix a[] , which is the matrix to be processed
for i = 1:n
%% first,check if a[i,1] is new
location_index = i+1;
for j = 1:i
if a[i,1] == height[j]
location_index = j;
break;
end if
end for %for j
%% second, compute or update the number of the rows with the same height
number[location_index]++;
%% third, compute or update the sum of the first and second colomn
sum_col_2[location_index] = sum_col_2[location_index] + a[i,2];
sum_col_3[location_index] = sum_col_3[location_index] + a[i,3];
end for %for i
%% forth, compute the average
for k = 1:n
avg_col_2[k] = sum_col_2[k]/number[k];
avg_col_3[k] = sum_col_3[k]/number[k];
end for %for k
这里提前假定各1维数组的对应元素之间的下标是一一对应的(实际上组成了一个结构体的形式,只是没有明确声明);输出的数组的个数应当是小于n的(考虑到重复的情况后),这里提前将输出数组大小设定为n,可以进一步细化和修改一下“第一部分”以使输出数组的大小根据实际结果而变化,亦可以在最后加入一个“截断去零”处理,使输出数组的大小根据实际结果而变化。
希望可以对你有些帮助。
Thx for reading.
PS:若还算满意,直接点击“Thanks”,再次登陆时亦便于查看回答是否真的帮到你了。
通过点击本人帖子旁边的ID 可以使用“发送悄悄话给silas_xue”与我进行联系
个人观点 仅供参考 多多交流 相互学习
此帖于 2009-08-12 17:15 被 silas_xue 编辑。
|