%% Reduce Spectral Dimensionality of Data Cube Using PCA
% Read a hyperspectral data into the workspace.

hcube = hypercube('paviaU.dat');

% Compute the principal component bands of the hyperspectral data cube. Specify the number of principal components to extract as 10. By default, the function uses the singular value decomposition (SVD) method for extracting principal components.

reducedDataCube = hyperpca(hcube,10);

% Display the first 10 spectral bands in input data cube.

figure
montage(hcube.DataCube(:,:,1:10),'BorderSize',[10 10],'Size',[2 5],'DisplayRange',[]);

% For the purpose of visualization, rescale the principal component values to lie in the range [0, 1]. Display all the principal component bands extracted from the data cube.

figure
rescalePC = rescale(reducedDataCube,0,1);
montage(rescalePC,'BorderSize',[10 10],'Size',[2 5]);
title('Principal Component Bands of Data Cube')

% Copyright 2020 The MathWorks, Inc.