%% Set N-FINDR Parameter Values for Finding Endmembers
% Read a hyperspectral data into the workspace.

hcube = hypercube('paviaU.hdr');

% Find the number of spectrally distinct endmembers present in the hyperspectral data cube by using countEndmembersHFC function.

numEndmembers = countEndmembersHFC(hcube,'PFA',10^-7);

% Compute the endmembers using the N-FINDR method. Specify the value for number of iterations as 1000. Select principal component analysis (PCA) as the dimensionality reduction method for preprocessing.

endmembers = nfindr(hcube.DataCube,numEndmembers,'NumIterations',1000,'ReductionMethod','PCA');

% Plot the endmembers of the hyperspectral data.

figure
plot(endmembers)
xlabel('Band Number')
ylabel('Pixel Values')
ylim([0 9000])
title({'Endmembers Spectra',['Number of Endmembers = ' num2str(numEndmembers)]});

% Copyright 2020 The MathWorks, Inc.