问题描述
我正在 Coursera 上做 Andrew Ng 的机器学习课程中的作业 3。该赋值带有一个 .mat,其中包含一个 5000x400 矩阵(表示为 X),其值介于 0 和 1 之间,对应于灰度。每行代表一个大小为 20x20 的图像。
该赋值带有一个预制函数,用于在一种称为 displayData(X,width) 的网格中显示图像。
问题是,这个函数只显示一个全黑的图形。由于这段代码是课程标准的,我根本没有弄乱它,我猜我的八度一定有问题。我正在使用 popOS 并通过带有“apt”的终端安装 Octave。我正在打开的外壳中使用 Octave 控制台。不管怎样,我会把函数的代码留在这里:
function [h,display_array] = displayData(X,example_width)
%disPLAYDATA display 2D data in a nice grid
% [h,display_array] = disPLAYDATA(X,example_width) displays 2D data
% stored in X in a nice grid. It returns the figure handle h and the
% displayed array if requested.
% Set example_width automatically if not passed in
if ~exist('example_width','var') || isempty(example_width)
example_width = round(sqrt(size(X,2)));
end
% Gray Image
colormap(gray);
% Compute rows,cols
[m n] = size(X);
example_height = (n / example_width);
% Compute number of items to display
display_rows = floor(sqrt(m));
display_cols = ceil(m / display_rows);
% Between images padding
pad = 1;
% Setup blank display
display_array = - ones(pad + display_rows * (example_height + pad),...
pad + display_cols * (example_width + pad));
% copy each example into a patch on the display array
curr_ex = 1;
for j = 1:display_rows
for i = 1:display_cols
if curr_ex > m,break;
end
% copy the patch
% Get the max value of the patch
max_val = max(abs(X(curr_ex,:)));
display_array(pad + (j - 1) * (example_height + pad) + (1:example_height),...
pad + (i - 1) * (example_width + pad) + (1:example_width)) = ...
reshape(X(curr_ex,:),example_height,example_width) / max_val;
curr_ex = curr_ex + 1;
end
if curr_ex > m,break;
end
end
% display Image
h = imagesc(display_array,[-1 1]);
% Do not show axis
axis image off
drawNow;
end
解决方法
使用命令 graphics_toolkit('gnuplot');
解决了问题!