top of page

Shannon Diversity Index

% Ehsan Momeni

 

% Shannon index calculator
% data must be in the following format : 
% GISJOIN Class1 Class2 .... (the first column must start with a letter)


%    GISJOIN       Class1    class2    class3    class4
% G47015700221224    0        297900    0        23400
% G47015700219002    28800    3125700    44100    294300

 

%% Environmet Preparation
format long;
clear all;
warning off;
clc;

%% Data Reading
[num,txt,raw] = xlsread(uigetfile('*.*'));
[D1 D2]=size(raw);

%% Header Modification
raw{1,D2+1}='Sum'; %sum of all classes 
for i=2:D2
    raw{1,D2+i}=['P_',raw{1,i}]; %Percentage of classes
end
raw{1,size(raw,2)+1}='Shannon_Index';
raw{1,size(raw,2)+1}='No_classes';
raw{1,size(raw,2)+1}='Shannon_Relative_Index';  
      
%% Putting zero value to all null cells for futer calculation
for i=2:D1
    for j=D2+1:size(raw,2)
        raw{i,j}=0;
    end
end

%% Calculation of sum (sum is the 'D2+1'th column)
for i=2:D1
    for j=2:D2
        raw{i,D2+1}=raw{i,D2+1}+raw{i,j};
    end
end

%% Getting rid of 0 values in sum: if sum of the area is 0 means area has no classed assigen to it
%  so we must get rid of them because they can cause probabily NaN and  index NaN, too

c=2; % a counter, starts from 2 because the first row is title line
for i=2:D1 %removing 0 row with Sum=0
    if raw{i,D2+1}~=0;
        for j=1:size(raw,2)
            raw2{c,j}=raw{i,j};
        end
        c=c+1;
    end    
end
for j=1:size(raw,2) % for the titel in the first column
    raw2{1,j}=raw{1,j};
end

clear raw; 
raw=raw2;
D1=size(raw2,1);
clear raw2;
%% Calcualtion of percentage of each class in an area
for i=2:D1
    for j=2:D2
        raw{i,D2+j}=raw{i,j}/raw{i,D2+1};
    end
end

%% calculation of Shannon Index for each area
for i=2:D1
    for j=2:D2
        if raw{i,D2+j}~=0;
         raw{i,2*D2+1}=raw{i,2*D2+1}+(raw{i,D2+j}*log(1/raw{i,D2+j}));
        end
    end
end

%% Calculatio of Number of classes in each area
for i=2:D1
    for j=D2+2:2*D2
        if raw{i,j}~=0;
          raw{i,2*D2+2}=raw{i,2*D2+2}+1;
        end
    end
end

%% calculartion of Shannon relative index for each area
for i=2:D1
        if raw{i,2*D2+2}>1;
          raw{i,2*D2+3}=raw{i,2*D2+1}/log(raw{i,2*D2+2});
        end
end

%% Averaging shannon indices to calculate the shannon index for whole area
Shannon_Index=0;
for i=2:D1
Shannon_Index=Shannon_Index+raw{i,2*D2+1};
end
Shannon_Index=Shannon_Index/(D1-1);


%% Averaging shannon relative indices to calculate the shannon relative index for whole area
Shannon_Relative_Index=0;
for i=2:D1
Shannon_Relative_Index=Shannon_Relative_Index+raw{i,2*D2+3};
end
Shannon_Relative_Index=Shannon_Relative_Index/(D1-1);


%% Messagebox
msgbox({['Shannon Index: ',num2str(Shannon_Index)] ['Shannon Relative Index: ',num2str(Shannon_Relative_Index)]});


%% Exporting Data

T = table(raw);
%T=cell2table(raw);
writetable(T,'Results.txt');

google_scholar1-300x150.png
ResearchGate.png
Ehsan Momeni ORCID GIS Remote Sensing Ur
Ehsan Momeni LinkedIn GIS Remote Sensing
ncbi-300x150.png
academia.png

(Information on this website may not be up to date)

bottom of page