fvecs_read.m 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. % Read a set of vectors stored in the fvec format (int + n * float)
  2. % The function returns a set of output vector (one vector per column)
  3. %
  4. % Syntax:
  5. % v = fvecs_read (filename) -> read all vectors
  6. % v = fvecs_read (filename, n) -> read n vectors
  7. % v = fvecs_read (filename, [a b]) -> read the vectors from a to b (indices starts from 1)
  8. function v = fvecs_read (filename, bounds)
  9. % open the file and count the number of descriptors
  10. fid = fopen (filename, 'rb');
  11. if fid == -1
  12. error ('I/O error : Unable to open the file %s\n', filename)
  13. end
  14. % Read the vector size
  15. d = fread (fid, 1, 'int');
  16. vecsizeof = 1 * 4 + d * 4;
  17. % Get the number of vectrors
  18. fseek (fid, 0, 1);
  19. a = 1;
  20. bmax = ftell (fid) / vecsizeof;
  21. b = bmax;
  22. if nargin >= 2
  23. if length (bounds) == 1
  24. b = bounds;
  25. elseif length (bounds) == 2
  26. a = bounds(1);
  27. b = bounds(2);
  28. end
  29. end
  30. assert (a >= 1);
  31. if b > bmax
  32. b = bmax;
  33. end
  34. if b == 0 | b < a
  35. v = [];
  36. fclose (fid);
  37. return;
  38. end
  39. % compute the number of vectors that are really read and go in starting positions
  40. n = b - a + 1;
  41. fseek (fid, (a - 1) * vecsizeof, -1);
  42. % read n vectors
  43. v = fread (fid, (d + 1) * n, 'float=>single');
  44. v = reshape (v, d + 1, n);
  45. % Check if the first column (dimension of the vectors) is correct
  46. assert (sum (v (1, 2:end) == v(1, 1)) == n - 1);
  47. v = v (2:end, :);
  48. fclose (fid);