0

I'm a beginner to MATLAB. I created a code that reads a dataset files with type of JPG (same file type) but with different sizes of each file. What I did I read 10 files randomly and put the contents of those files into a matrix (with type of cell array) called 'fr' (as showing below). This 'fr' contains 10 columns which means 10 files that were read. The contents of files that I extracted (read) is binary file byte (0-255) However, I extracted the contents of each file and put them into a variable called 'out1' but the problem was how to access those contents with different file sizes for each file and I already did solve it by using a function (cellfun) and I filled in the blank cell by adding zeros. Eventually, the matrix worked, but the problem I face now is how to subtract this matrix 'out1' from the whole files that were read? I'm stuck on this part. This error says that the matrix dimensions must agree: as explained at the end.

Here is the output: ??? Error using ==> minus Matrix dimensions must agree.

Error in ==> PCATEST2 at 50
B = (out1 - repmat(AMean,[n 1])) / repmat(AStd,[n 1]);

EDU>> whos
  Name                Size              Bytes  Class     Attributes

  AMean               1x10                 80  double              
  AStd                1x10                 80  double              
  ans                 1x1                   8  double              
  f                   1x57                114  char                
  fid                 1x1                   8  double              
  files              50x1               32870  struct              
  fr                  1x10            6715472  cell                
  i                   1x1                   8  double              
  j                   1x1                   8  double              
  m                   1x1                   8  double              
  maxLength           1x1                   8  double              
  n                   1x1                   8  double              
  out1           101077x10            8086160  double              
  ridx               50x1                 400  double              
  st1                 1x10               1460  cell

This the code:

f ='/Users/nsa/Documents/MATLAB/jpg-data1/';
files = dir(fullfile(f,'*.jpg'));
ridx = randi(numel(files),size(files)); %choose files from f randomly

 for i = 1:10  %randomly pick 10 files
 st1(i) = {files(ridx(i)).name}; 

  for j= i:length(st1)

    fid = fopen(fullfile(f, st1{i}),'rt');

    fr{j} = fread(fullfile(fid));

    fclose(fid); 

 end

end
maxLength=max(cellfun(@(fr)numel(fr),fr));
out1 = cell2mat(cellfun(@(fr)cat(1,fr,zeros(maxLength-length(fr),1)),fr,'UniformOutput',false));

[n m] = size(fr);

AMean = cellfun(@mean,fr);

AStd = cellfun(@std,fr);

B = (out1 - repmat(AMean,[n 1])) / repmat(AStd,[n 1]);


??? Error using ==> minus
Matrix dimensions must agree.

Error in ==> TEST2 at 50
B = (out1 - repmat(AMean,[n 1])) / repmat(AStd,[n 1]);

Can anyone help me and guide me to fix and overcome this error?

1 Answers1

1

You should do:

[n m] = size(out1); 

since fr has only 1 row and out1 quite a few.

However, you can even skip the repmatting by using bsxfun():

B = bsxfun(@rdivide, bsxfun(@minus,out1,AMean), AStd);
Oleg
  • 10,406
  • 3
  • 29
  • 57