How to append a 3D array inside a parfor in matlab?

3 views (last 30 days)
I have several gray-scale images and I want to store that in a 3d array(hieghtXwidthXnumber-of-images) in matlab.
my code looks like this
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
Error: The temporary variable train_img in a parfor is uninitialized. See Parallel for Loops in MATLAB, "Uninitialized Temporaries".
In the above code the parfor i=1: 100, I don't know whats the upper limit of loop. Its decided at run time. Could anybody let me know what this error means and how to overcome this?

Answers (1)

Walter Roberson
Walter Roberson on 16 Jun 2016
You should be writing the array to train_img{i} instead of trying to do the cat(3). After the parfor you can run through the cell array and find the largest image and pad everything out to that size and write it all to a 3D array.
Alternately you could write to train_img(:,:,i) in the parfor loop, but only if you also added code that handles the possibility that the images are not all the same size.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!