首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何计算数组中列的平均值?

如何计算数组中列的平均值?
EN

Stack Overflow用户
提问于 2013-03-13 04:24:57
回答 1查看 1.8K关注 0票数 0

我目前正在做Matlab中的一个项目,其中我有一个单元阵列。第一个单元阵列长464列,深1行。这些单元中的每一个都是96列和365行的另一个单元阵列。我需要能够获得464个数组中每个数组的96列的平均值,并将464个数组中的每个数组放在一个名为mean的新数组中的不同行。我试着编写代码,只做一个专栏,如下所示:

代码语言:javascript
代码运行次数:0
运行
复制
    mean = Homes{1,1}(1:)

但是当我尝试运行这段代码时,我得到了以下错误:

代码语言:javascript
代码运行次数:0
运行
复制
    mean = Homes{1,1}(1:)
                       |
    Error: Unbalanced or unexpected parenthesis or bracket.

基本上,我最终的数组名称均值需要是96列乘以464行。我被困住了,真的需要你的帮助。

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2013-03-13 06:43:16

我建议您在较小的矩阵上尝试以下代码。看看它是否能给你想要的结果。

代码语言:javascript
代码运行次数:0
运行
复制
a=cell(1,4); %for you it will be a=cell(1,464)
for i=1:4
   a{i}=randi(10,[5 10]); %for you it will be a{i}=randi(10,[365 96]);
end
a1=cell2mat(a);  %just concatenating
a1=mean(a1); %getting the mean for each column. in your case, you should get the mean for 96*464
a2=reshape(a1,[10 4]); %now what reshape does it it takes first 10 elements and arranges it into first column.
%Therefore, since I finally want a 4x10 matrix (in your case 464x96), i.e. mean of 10 elements in first cell array on first row and so on...
%Thus, 1st 10 elements should go to first column after doing reshape (since you want to keep them together). Therefore, instead of directly making matrix as 4x10, first make it as 10x4 and then take transpose (which is the next step).

a2=a2'; %thus you get a 4x10 matrix.

特别是在您的情况下,代码将是

代码语言:javascript
代码运行次数:0
运行
复制
a=cell(1,464); 
for i=1:464
   a{i}=randi(10,[365 96]); 
end
a1=cell2mat(a);  
a1=mean(a1); 
a2=reshape(a1,[96 365]);                        
a2=a2'; 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15371406

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档