MATLAB Answers

Adapting an existing Matlab Function to compute frequencies from table amounts

1 view (last 30 days)
Darren Johnson
Darren Johnson on 11 Sep 2019
Answered: Mrunmayee Gaikwad on 27 Aug 2020
I am currently attempting to adapt existing an existing matlab code (sucessfully used to create gini coefficients using .dbf output from a histogram) to perform the same function for a simple table count in excel. The code is attached below. The purpose of this script is explained in the code itself, but the basic idea is it would convert count numbers for "income" levels into frequencies, and then perform the necessary steps to compute a Gini coefficient for that.
My aim would be to modify this script so that it can perform the same process, but for an excel table which contains similar data (one column for "income", one column for "count" of said income levels).
I have attached an excel document which contains a table - lightcount is the "income", and "count" is the frequency by which "income" value was found in the dataset. I attempted to adapt the code by simply trying to replace to_frequency(hist) with the lightcount variable name, but that produced no results.
Thank you for your time.
% Takes a matrix of histogram amounts and normalize them by dividing each
% column by the total of that column
function freq = to_frequency(input)
a = sum(input);
totals = repmat(a,size(input,1),1);
freq = input./totals;
Code for "calculating" the gini itself
% Function to calculate the Gini coefficient taking as input an histogram
% INPUTS x_values : the labels of the histograms values (y)
% hist : the amount (count) for every value (f(y))
% OUTPUT G : the Gini coefficient function G = light_gini(x_values,hist)
% converts the histogram counts in frequencies
list = to_frequency(hist);
% computes f(y)*y
s = list.*x_values;
% computes the cumulative summation
s = cumsum(s);
% workaround to get S0=0
s2 = [0; s];
s = [s; 0];
% get the second part of the numerator
s3 = s+s2;
list = [list; 0];
% compute the Gini coefficient
G = 1 - sum(list.*s3)/s2(end);

Answers (1)

Mrunmayee Gaikwad
Mrunmayee Gaikwad on 27 Aug 2020
The MATLAB function 'sum' and operation './' (element wise division), used in the 'to_frequency()' function is not applicable for a 'table' variable as input. They work for elements, vectors and matrices. So, to use the function on data imported from excel sheet, you need to convert the table into matrix as following:
t = readtable('stats1992NAME_EN_Hong Kong.xlsx'); % importing the excel sheet data into a table named 't'
tMatrix = t.Variables; % extracting the data from table 't' into a matrix 'tMatrix'
list = to_frequency(tMatrix); % passing the matrix as input to the function
For more information, refer the documentation on 'table' which contains examples on accessing table data as matrix:


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!