%% Extract Endmembers Using N-FINDR Method
% 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. By default, the nfindr function uses maximum noise fraction (MNF) transform for preprocessing. The default value for number of iterations is 3 times the number of estimated endmembers.

endmembers = nfindr(hcube.DataCube,numEndmembers);

% 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.