使用repmat进行结构初始化 [英] Structure initialization with repmat

查看:69
本文介绍了使用repmat进行结构初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想初始化结构,这似乎太慢了. 如何如何使用 repmat 做到这一点,在Matlab中这应该是更快的解决方案? 原来是:

I want to initalize structures and it seems to be too slow. How do I do it with repmat, which is supposed to be a much faster solution in Matlab? Originally:

for i=1:30
    myloc.one.matrixBig(i,1).matrixBig= zeros(6,6);
    for j=1:5
      myloc.one.id(i,j) = 0;
      for k=1:10
          myloc.one.final(j,k).final(i,1) = 0;
      end
    end
end


   for j=1:30
       for i=1:10 
          myObject{i,j}.s = zeros(6,1);
          myObject{i,j}.f = zeros(6,1);
       end
   end

此外,我是否可以通过甚至在之前添加一些[]初始化来使其变得更快,或者这是我的优化可能性的极限? 非常感谢您的帮助!

Also, am I able to make it faster by adding some [] initialization even before, or is that limit of my optimization possibilities? Thanks very much for help!

推荐答案

以下是第一个代码段的等效矢量化代码:

Here's the equivalent vectorized code of the first code snippet:

myloc.one = struct('id', zeros(30, 5), ...
  'matrixBig', struct('matrixBig', repmat({zeros(6)}, 30, 1)), ...
  'final', struct('final', repmat({zeros(30, 1)}, 5, 10)));

或者:

myloc.one = struct('id', zeros(30, 5), ...
   'matrixBig', repmat(struct('matrixBig', zeros(6)), 30, 1), ...
   'final', repmat(struct('final', zeros(30, 1)), 5, 10));

选择最喜欢的人.

对于第二部分(已编辑),可以将其替换为:

As for the second (edited) part, it can be replaced with:

myObject = repmat({struct('s', zeros(6, 1), 'f', zeros(6, 1))}, 30, 10);

请注意,由于此处没有任何显式循环,因此无需预先分配任何内容.

Note that there is no need to preallocate anything because there aren't any explicit loops here.

这篇关于使用repmat进行结构初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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