Path: blob/master/Week 4/Programming Assignment - 3/machine-learning-ex3/ex3/displayData.m
864 views
function [h, display_array] = displayData(X, example_width)1%DISPLAYDATA Display 2D data in a nice grid2% [h, display_array] = DISPLAYDATA(X, example_width) displays 2D data3% stored in X in a nice grid. It returns the figure handle h and the4% displayed array if requested.56% Set example_width automatically if not passed in7if ~exist('example_width', 'var') || isempty(example_width)8example_width = round(sqrt(size(X, 2)));9end1011% Gray Image12colormap(gray);1314% Compute rows, cols15[m n] = size(X);16example_height = (n / example_width);1718% Compute number of items to display19display_rows = floor(sqrt(m));20display_cols = ceil(m / display_rows);2122% Between images padding23pad = 1;2425% Setup blank display26display_array = - ones(pad + display_rows * (example_height + pad), ...27pad + display_cols * (example_width + pad));2829% Copy each example into a patch on the display array30curr_ex = 1;31for j = 1:display_rows32for i = 1:display_cols33if curr_ex > m,34break;35end36% Copy the patch3738% Get the max value of the patch39max_val = max(abs(X(curr_ex, :)));40display_array(pad + (j - 1) * (example_height + pad) + (1:example_height), ...41pad + (i - 1) * (example_width + pad) + (1:example_width)) = ...42reshape(X(curr_ex, :), example_height, example_width) / max_val;43curr_ex = curr_ex + 1;44end45if curr_ex > m,46break;47end48end4950% Display Image51h = imagesc(display_array, [-1 1]);5253% Do not show axis54axis image off5556drawnow;5758end596061