Search code examples
arraysmatlaboctavecell

How does a one liner in matlab cell array works?


SUBSAMPLE = 2; % Subsample your image by this much
for person_num = 1:40
  for sample_num = 1:10
  filename = ['orl_faces/s' num2str(person_num) '/'  num2str(sample_num) '.pgm'];
    if sample_num < 8
      img = imread(filename);
      [m,n] = size(img);
      imagetrain{person_num,sample_num} = img(1:SUBSAMPLE:m,1:SUBSAMPLE:n)(:);
    else
      img = imread(filename);
      imagetest{person_num,sample_num} = img(1:SUBSAMPLE:m,1:SUBSAMPLE:n)(:);
    end
  end
end

Above is code fragment from a matlab code for facial recognition. I am new to matlab therefore I am trying to understand statements that are new to me. My question is, imagetrain and imagetest is being assigned something from img(). Can anyone explain to me what img() is being assigned to the matrices? It looks like something of oneliner to me.


Solution

  • img is the image loaded using imread.

    The code img(1:SUBSAMPLE:m,1:SUBSAMPLE:n) subsamples the image and reduces its size. The result is a new image (i.e. a new matrix) with fewer number of rows and fewer number of columns.

    The leading (:) converts (reshapes) the matrix into a column vector. So, for example if the original image is 256*256, the subsampled image will be 128*128, and after (:) it will become a column vector with 128*128=16384 elements.