【问题标题】:How to append a 3D array inside a parfor in matlab?如何在matlab的parfor中附加一个3D数组?
【发布时间】:2016-06-16 04:07:38
【问题描述】:

我有几个灰度图像,我想将它们存储在 matlab 中的 3d 数组(hieghtXwidthXnumber-of-images)中。

我的代码是这样的

train_img = [];
parfor i=1:100
      a = imread(image-file);
      a1 = imresize(a, 0.5);
      b = rgb2gray(a1);
      d = im2double(b);
      train_label = [train_label;p];
      train_img = cat(3,train_img(:,:,:),d);
end

错误:parfor 中的临时变量 train_img 未初始化。 请参阅 MATLAB 中的并行 for 循环,“未初始化的临时文件”。

上面代码parfor i=1:100,不知道循环的上限是多少。它在运行时决定。 谁能告诉我这个错误是什么意思以及如何克服这个错误?

【问题讨论】:

    标签: matlab multidimensional-array append parfor


    【解决方案1】:

    你在表达式的左边和右边写上train_img。因此,下一次迭代取决于train_img 的先前值。这可以防止 MATALB 使用parfor。如果您将代码修改如下,它应该可以工作:

    parfor i=1:your_upper_bound %it can be a variable too, just define it
          a = imread(image_file);
          a1 = imresize(a, 0.5);
          b = rgb2gray(a1);
          d = im2double(b);
          train_label(i,1) = p; % if this doesn't work -> define train_label before parfor
          train_img(:,:,i) = d; % if this doesn't work -> define train_img before parfor
    end
    

    【讨论】:

    • 感谢您的回答。是否可以在不使用索引的情况下将二维数组附加到三维的 train_img(3d 数组)?就像我想将使用 i 的 train_img(:,:,i) = d 替换为 train_label = [train_label;p]。有可能吗?
    • @SurjyaNarayanaPadhi 我认为答案的重点是你不能在左右两边都有train_label,所以没有
    • @SurjyaNarayanaPadhi 使用 parfor 时是不可能的,但我不明白你为什么需要它。你能告诉我一个你认为你需要它的场景吗?
    猜你喜欢
    • 2015-10-15
    • 2015-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-31
    • 2015-04-09
    • 2012-01-06
    • 1970-01-01
    相关资源
    最近更新 更多