Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 用repmat初始化结构_Matlab_Struct_Initialization_Vectorization - Fatal编程技术网

Matlab 用repmat初始化结构

Matlab 用repmat初始化结构,matlab,struct,initialization,vectorization,Matlab,Struct,Initialization,Vectorization,我想初始化结构,但它似乎太慢了我如何使用repmat进行计算,这在Matlab中应该是一个更快的解决方案? 原来: 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

我想初始化结构,但它似乎太慢了我如何使用repmat进行计算,这在Matlab中应该是一个更快的解决方案? 原来:

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
此外,我是否能够通过在之前添加一些[]初始化来加快速度,或者这是我优化可能性的极限?
非常感谢你的帮助

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

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));
选择一个你最喜欢的

对于第二个(已编辑的)零件,它可以替换为:

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

请注意,不需要预先分配任何内容,因为这里没有任何显式循环。

在这种情况下,第一个问题是为什么要使用结构的单元数组。matlab的一个基本原理是一切都可以矢量化。因此,相反,建立一个结构矩阵

% Define the empty struct, struct itself is vectorised   
myloc=struct('s',cell(10,30),'f',cell(10,30));
%Assign values
[myloc(:).s]=deal(0),[myloc(:).f]=deal(0);
结构矩阵是一种更强大的数据类型。例如,如果要将字段的一行提取到向量中,只需使用以下语法

sRow8=[myloc(8,:).s]

谢谢,你能用手机回答我在帖子中更新的案例的解决方案吗?我很抱歉问了这么多琐碎的问题,但在这种情况下,matlab的帮助在解释方面对我来说并不好……是的,我当然很乐意这样做!如果我的单元格中有.s和.f数组,请您再更新一次,并说明在哪里进行更改?谢谢在哪里学习如何使用repmat?@beginh了解语法的最佳方法是阅读(以下是到和的链接)。非常感谢!我迫切需要它,但现在我要自己学习,这样我就不会问所有的事情了。@beginh没问题:)但你应该知道,在结构单元中真的没有意义。一个结构数组可以实现相同的效果和更多的效果,并且占用的内存更少(比一个结构单元数组)。如果你去掉我问题第二部分中的大括号(
{}
),你将得到你的结构数组。