具有数组的结构域的元素明智平均 [英] Element wise average of structure fields with arrays

查看:69
本文介绍了具有数组的结构域的元素明智平均的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为"xyz"的结构,其中包含以下内容.如何制作一个新的变量(141x136的两倍),具有字段的元素取平均值.

I have a structure called "xyz" with the following contents. How can I make a new variable (a 141x136 double) having an element wise average of the fields.

推荐答案

您可以按照以下步骤操作:

You can follow these steps:

  1. 将结构转换为单元格数组.
  2. 从单元格生成一个逗号分隔的列表,并沿三维连接单元格的内容,从而生成3D数字数组.
  3. 计算第3维的平均值.

示例:

xyz.v1 = ones(5,6);
xyz.v2 = 2*ones(5,6);  % example data
c = struct2cell(xyz);  % step 1
c = cat(3, c{:});      % step 2
result = mean(c, 3);   % step 3

考虑一下,如果您真的需要一个结构.也许您可以改为使用3D数组(取决于代码的其余部分).通常,这会节省更多时间和内存.这些字段将对应于3rd-dim切片,您只需要上面的步骤3,即mean(..., 3).

Consider if you really need a struct. Maybe you can have the 3D array instead (depending on the rest of your code). That's usually more time- and memory-efficient. The fields would correspond to the 3rd-dim slices, and you would only need step 3 above, that is, mean(..., 3).

这篇关于具有数组的结构域的元素明智平均的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆