

%% firstly please download the data from http://yann.lecun.com/exdb/mnist/
%% then please put the downloaded file in your current work directory

te_label_file = 't10k-labels.idx1-ubyte';
tr_label_file = 'train-labels.idx1-ubyte';
te_data_file  = 't10k-images.idx3-ubyte';
tr_data_file  = 'train-images.idx3-ubyte';


% read training labels
fid = fopen(tr_label_file,'r');
B = fread(fid); fclose(fid);
%60000 training labels are the last 60000 bytes
tr_label = B(end-60000+1:end);


% read test labels
fid = fopen(te_label_file,'r');
B = fread(fid); fclose(fid);
%10000 test labels are the last 10000 bytes
te_label = B(end-10000+1:end);


% read the training data
fid = fopen(tr_data_file,'r');
B = fread(fid); fclose(fid);
%ignore the headers 4x4 bytes
B = B(17:end); 
tr_feats = reshape(B,28*28,60000)';


% read the test data
fid = fopen(te_data_file,'r');
B = fread(fid); fclose(fid);
%ignore the headers 4x4 bytes
B = B(17:end); 
te_feats = reshape(B,28*28,10000)';

% display the first 10 digits in the training set
for i=1:10
    subplot(2,5,i);
    im=tr_feats(i,:);
    im=reshape(im,28,28);
    im=im';
    imshow(uint8(im));
end

