![]() |
如何在特殊数组中求取均值
请问:有这样一个数组,内容为
5 -11.6 62.7 7 -11.7 63.2 8 -11.7 62.1 8 -11.5 65 8 -11.6 65 9 -11.7 67.2 10 -11.7 66.5 11 -11.7 67.1 11 -11.7 68 13 -11.6 67.8 ..... 第一列是高度,可见有的同一高度如8出现3次,如何对这个矩阵数组处理,使输出结果不同高度只出现一次,且第2列、第3列数是对应高度上n次的平均值。如 5 -11.6 62.7 7 -11.7 63.2 8 -11.6 64.3 9 -11.7 67.2 10 -11.7 66.5 11 -11.7 67.55 13 -11.6 67.8 谢谢指导! |
回复: 如何在特殊数组中求取均值
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”与我进行联系 个人观点 仅供参考 多多交流 相互学习 |
所有时间均为北京时间。现在的时间是 11:31。 |
Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.