Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/Coursera-Machine-Learning
Path: blob/master/Week 8/Programming Assignment - 7/ex7/displayData.m
863 views
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
7
% Set example_width automatically if not passed in
8
if ~exist('example_width', 'var') || isempty(example_width)
9
example_width = round(sqrt(size(X, 2)));
10
end
11
12
% Gray Image
13
colormap(gray);
14
15
% Compute rows, cols
16
[m n] = size(X);
17
example_height = (n / example_width);
18
19
% Compute number of items to display
20
display_rows = floor(sqrt(m));
21
display_cols = ceil(m / display_rows);
22
23
% Between images padding
24
pad = 1;
25
26
% Setup blank display
27
display_array = - ones(pad + display_rows * (example_height + pad), ...
28
pad + display_cols * (example_width + pad));
29
30
% Copy each example into a patch on the display array
31
curr_ex = 1;
32
for j = 1:display_rows
33
for i = 1:display_cols
34
if curr_ex > m,
35
break;
36
end
37
% Copy the patch
38
39
% Get the max value of the patch
40
max_val = max(abs(X(curr_ex, :)));
41
display_array(pad + (j - 1) * (example_height + pad) + (1:example_height), ...
42
pad + (i - 1) * (example_width + pad) + (1:example_width)) = ...
43
reshape(X(curr_ex, :), example_height, example_width) / max_val;
44
curr_ex = curr_ex + 1;
45
end
46
if curr_ex > m,
47
break;
48
end
49
end
50
51
% Display Image
52
h = imagesc(display_array, [-1 1]);
53
54
% Do not show axis
55
axis image off
56
57
drawnow;
58
59
end
60
61