Search code examples
matlabmatrixmultidimensional-arrayreshapelocality-sensitive-hash

Matlab: reshape 4-d matrix to 2-d and maintain order, how to?


I'm trying to implement vlsh with the California ND Dataset, which is composed by 701 photos. 10 subject wrote down in a txt file which photos are near duplicate for them, and we have also correlation matrix. The images are RGB and I reduced them in 20x20. I created a 4-d array 20x20x3x701. So I tried to reshape and obtained a 1200x701 matrix, but the problem is that reshape can't maintain the order of the original matrix. I tried to search online and most of suggestion is to use "Permute", but it seems to me that doesn't fit my situation.

I can post the MATLAB code:

path='C:\Users\franc\Desktop\stage\californiaND\prova*.jpg';
path2='C:\Users\franc\Desktop\stage\californiaND\prova';
d=dir(path);
a=[];
for m=1:length(d)
  a=cat(4,a,imread(strcat(path2,d(m).name)));
end
r=reshape(a,[],701);
r=double(r);
L = lshConstruct( r, 10,4);

Solution

  • I am assuming you need the 1D vector in RGBRGB format, as otherwise the solution would be trivial ( just (:)).

    Assuming imread is reading 20x20x3 images one by one, this is how you directly make your 2D matrix:

    for m=1:length(d) % this is 701, right?
      im=imread(strcat(path2,d(m).name));
      im=permute(im,[3 1 2]);
      a=cat(2,im(:));
    end