displayData.m 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. function [h, display_array] = displayData(X, example_width)
  2. %DISPLAYDATA Display 2D data in a nice grid
  3. % [h, display_array] = DISPLAYDATA(X, example_width) displays 2D data
  4. % stored in X in a nice grid. It returns the figure handle h and the
  5. % displayed array if requested.
  6. % Set example_width automatically if not passed in
  7. if ~exist('example_width', 'var') || isempty(example_width)
  8. example_width = round(sqrt(size(X, 2)));
  9. end
  10. % Gray Image
  11. colormap(gray);
  12. % Compute rows, cols
  13. [m n] = size(X);
  14. example_height = (n / example_width);
  15. % Compute number of items to display
  16. display_rows = floor(sqrt(m));
  17. display_cols = ceil(m / display_rows);
  18. % Between images padding
  19. pad = 1;
  20. % Setup blank display
  21. display_array = - ones(pad + display_rows * (example_height + pad), ...
  22. pad + display_cols * (example_width + pad));
  23. % Copy each example into a patch on the display array
  24. curr_ex = 1;
  25. for j = 1:display_rows
  26. for i = 1:display_cols
  27. if curr_ex > m,
  28. break;
  29. end
  30. % Copy the patch
  31. % Get the max value of the patch
  32. max_val = max(abs(X(curr_ex, :)));
  33. display_array(pad + (j - 1) * (example_height + pad) + (1:example_height), ...
  34. pad + (i - 1) * (example_width + pad) + (1:example_width)) = ...
  35. reshape(X(curr_ex, :), example_height, example_width) / max_val;
  36. curr_ex = curr_ex + 1;
  37. end
  38. if curr_ex > m,
  39. break;
  40. end
  41. end
  42. % Display Image
  43. h = imagesc(display_array, [-1 1]);
  44. % Do not show axis
  45. axis image off
  46. drawnow;
  47. end