Matlab将矩阵合并到单元格

时间:2016-04-14 14:06:38

标签: matlab matrix cell-array

在Matlab中,我得到了三个矩阵(由向量组成)x, y, z,每个大小为3xn

我想将它们合并到包含n个条目的单元格,每个条目都是3x3矩阵:

for i=1:n
    C{i} = [x(:,i), y(:,i), z(:,i)];
end

是否有比使用此循环更快的方法,因为这需要很长时间?

我已经找到了像mat2cellcellfun这样的功能,但它们都没有真正做到我需要的功能,是吗?

3 个答案:

答案 0 :(得分:4)

除非您调用的代码需要它,否则请使用3D数组。它的开销要小得多。

C = zeros(3, 3, n);
for ii = 1:n
    C(:, :, ii) = [x(:,ii) y(:,ii) z(:,ii)];
end

在此表单中,您可以使用reshape

对其进行矢量化
C = reshape([x; y; z], [3 3 n]);

答案 1 :(得分:2)

试试这个:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="MyModule">
  <div ng-controller="MyController as ctrl">
    <!-- Create input radio button -->
    <input type="radio" name="color" ng-model="ctrl.colorSelected" ng-value="ctrl.bgColorRed">Red

    <input type="radio" name="color" ng-model="ctrl.colorSelected" ng-value="ctrl.bgColorPink">Pink

    <input type="radio" name="color" ng-model="ctrl.colorSelected" ng-value="ctrl.bgColorOrange">Orange

    <input type="radio" name="color" ng-model="ctrl.colorSelected" ng-value="ctrl.bgColorYellow">Yellow

    <div class="box" ng-class="{red: ctrl.colorSelected == ctrl.bgColorRed, pink: ctrl.colorSelected == ctrl.bgColorPink, orange: ctrl.colorSelected == ctrl.bgColorOrange, yellow: ctrl.colorSelected == ctrl.bgColorYellow}">
      {{ctrl.colorSelected}} Letterpress craft beer typewriter, bitters butcher ennui heirloom celiac. Four dollar toast pork belly 8-bit trust fund, raw denim letterpress shoreditch stumptown food truck locavore venmo typewriter blog. Post-ironic chambray
      lumbersexual, fashion axe hoodie kitsch swag yuccie organic. DIY hoodie lomo, austin post-ironic literally portland shoreditch pour-over neutra sriracha YOLO selvage thundercats messenger bag.
    </div>
    <div class="box" ng-class="{red: ctrl.colorSelected == ctrl.bgColorRed, pink: ctrl.colorSelected == ctrl.bgColorPink, orange: ctrl.colorSelected == ctrl.bgColorOrange, yellow: ctrl.colorSelected == ctrl.bgColorYellow}">
      Tumblr hammock authentic, humblebrag pitchfork ramps listicle cliche distillery ethical 8-bit vice. 3 wolf moon whatever direct trade fanny pack franzen, swag polaroid austin letterpress. Street art health goth everyday carry heirloom hoodie echo park
      gluten-free irony, viral venmo brunch vegan pop-up. Man braid listicle food truck, fashion axe austin polaroid pop-up shoreditch post-ironic scenester jean shorts synth.
    </div>
  </div>
</body>

答案 2 :(得分:0)

您可以预先分配内存以提高性能:

C = cell(n, 1);