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)
cols = sum(a,1)
b = a;
i = 1;
while sum(b,1,"omitnan") ~= sum(b,2,"omitnan")'
b = b ./ sum(b,1,"omitnan");
b = b ./ sum(b,2,"omitnan");
i = i + 1;
brows = sum(b,2,"omitnan")
bcols = sum(b,1,"omitnan")
attached are two 7 x 7 matrices. These are the desired input for a.
The margfit function (row 345 - 376) in link below, is (I think) what I am trying to implement. My python is non-existant