Row & Column Wise Normalisation
28 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
edward holt
el 12 de Feb. de 2020
Comentada: edward holt
el 13 de Feb. de 2020
Objective: Normalise a matrix such that all rows and columns sum to 1.
The below normalises each column, then row and repeats until row and column totals, equal one another.
This seems to work for randomly generated arrays.
However, the data I wish to use it on has some zeros - and that is generating lots of NaN and Infs, which is making things quite messy and sometimes when running the while loop won't execute (no error message, it just hops over it)
I've tried changing the while condition to be rounded to 3 decimal places (because that's good enough) but still no success.
a = rand(7)
rows = sum(a,2) % orginal row totals
cols = sum(a,1) % original col totals
b = a;
i = 1; % for counting how many iterations
while sum(b,1,"omitnan") ~= sum(b,2,"omitnan")' %when column totals == row totals, stop.
b = b ./ sum(b,1,"omitnan"); %divide by col totals
b = b ./ sum(b,2,"omitnan"); %divide by row totals
i = i + 1;
end
i %how many loops
b % normalised output
brows = sum(b,2,"omitnan") %check that all rows sum 1
bcols = sum(b,1,"omitnan") %check that all cols sum 1.
attached are two 7 x 7 matrices. These are the desired input for a.
Suggestions welcome.
edit:
The margfit function (row 345 - 376) in link below, is (I think) what I am trying to implement. My python is non-existant
9 comentarios
Respuesta aceptada
Matt J
el 13 de Feb. de 2020
Editada: Matt J
el 13 de Feb. de 2020
For a non-negative square matrix, the attached article mentions necessary and sufficient conditions (p. 3, Theorem 1) both for the normalization you are trying to achieve to be possible and for the alternating row/column normalization approach (the Sinkhorn-Knopp algorithm ) to work. The required condition for both are the same. So basically, if you are seeing Infs and NaNs in your iterations, the normalization is known to be impossible from the get-go.
The condition is:
"A necessary and sufficient condition ... is that A has total support"
The given matrix A having total support means that for every non-zero element A(i,j)>0, a column permutation Ap of A exists such that Ap has only strictly positive elements on the diagonal, one of which is A(i,j).
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!