Discussion:
Normalizing matrix (rows AND columns equal to 1)
(too old to reply)
zuzia
2012-11-21 22:55:13 UTC
Permalink
I'm working on a specific matrix and I need to normalize the sum of the elements in all the rows and all the columns to be equal to 1. I have managed to create the codes to do both separately but does anyone know how to obtain both simultaneously?
thank you!!!
dpb
2012-11-21 23:30:11 UTC
Permalink
Post by zuzia
I'm working on a specific matrix and I need to normalize the sum of the
elements in all the rows and all the columns to be equal to 1. I have
managed to create the codes to do both separately but does anyone know
how to obtain both simultaneously?
thank you!!!
Taking "all the rows and all the columns" literally,

x=x/sum(x(:));

If you mean each row and column simultaneously to sum to 1, otomh I
don't think there's a guarantee that has a solution; at least w/ linear
weights.

It would be S_j(w(i,j).*x(i,j)) + S_i(w(i,j).*x(i,j)) = 2

and try to solve for w

--
zuzia
2012-11-21 23:58:11 UTC
Permalink
Yes, I meant EACH of them equal to 1, not all of them simultaneously.
Thank you for your reply but i dont really understand your result... So far I came up with:

B = A;
columnSums = sum(B);
for i = 1:numel(columnSums)
B(:,i) = B(:,i)./columnSums(i);
end
(for summing the columns)

and
B = A;
rowSums = sum(transpose (A));
for j = 1:numel(rowSums)
B(j,:) = B(j,:)./rowSums(j);
end
(for summing the rows).

Do you think it's possible just to impose a condition applying them both together? Or otherwise maybe you could please explain your command?
Post by dpb
Post by zuzia
I'm working on a specific matrix and I need to normalize the sum of the
elements in all the rows and all the columns to be equal to 1. I have
managed to create the codes to do both separately but does anyone know
how to obtain both simultaneously?
thank you!!!
Taking "all the rows and all the columns" literally,
x=x/sum(x(:));
If you mean each row and column simultaneously to sum to 1, otomh I
don't think there's a guarantee that has a solution; at least w/ linear
weights.
It would be S_j(w(i,j).*x(i,j)) + S_i(w(i,j).*x(i,j)) = 2
and try to solve for w
--
dpb
2012-11-22 01:24:06 UTC
Permalink
On 11/21/2012 5:58 PM, zuzia wrote:

...[top posting repaired--please don't do that--hard follow conversation
makes]...
Post by zuzia
Post by dpb
Post by zuzia
I'm working on a specific matrix and I need to normalize the sum of the
elements in all the rows and all the columns to be equal to 1. I have
managed to create the codes to do both separately but does anyone know
how to obtain both simultaneously?
thank you!!!
Taking "all the rows and all the columns" literally,
x=x/sum(x(:));
If you mean each row and column simultaneously to sum to 1, otomh I
don't think there's a guarantee that has a solution; at least w/
linear weights.
It would be S_j(w(i,j).*x(i,j)) + S_i(w(i,j).*x(i,j)) = 2
and try to solve for w
Yes, I meant EACH of them equal to 1, not all of them simultaneously.
B = A;
columnSums = sum(B);
for i = 1:numel(columnSums)
B(:,i) = B(:,i)./columnSums(i);
end
(for summing the columns)
and B = A;
rowSums = sum(transpose (A));
for j = 1:numel(rowSums)
B(j,:) = B(j,:)./rowSums(j);
end
(for summing the rows).
Post by dpb
Do you think it's possible just to impose a condition applying them both
together? Or otherwise maybe you could please explain your command?
I don't see how. You've got nXm coefficients to determine from only n+m
equations.

As for the above, you can write it as

B=A./repmat(sum(A),size(A,1),1); % columns
B=A./repmat(sum(A,2),1,size(A,2)); % columns

I'm still not clear on what you really mean w/ "not all simultaneously"
and then "applying both together". It seems to be s dichotomy.

But if you mean just one row/column pair at a time, consider a 2x2
example. You'd have

x(1,1)*w(1,1)+x(2,1)*w(2,1)=1 % 1st column
x(1,1)*w(1,1)+x(1,2)*w(1,2)=1 % 1st row

That's determining three unknown weights w/ only two equations and as
the array gets larger you add more weights but no more constraints.

--
dpb
2012-11-22 02:58:53 UTC
Permalink
On 11/21/2012 7:24 PM, dpb wrote:
...
But if you mean just one row/column pair at a time,...
Actually, if you can stand it, you could get the sums to match by
choosing either column or row first, normalize it by the sum then omit
the intersecting element for the corresponding row/column and normalize
the remaining elements to 1-intersecting_value

That would end up w/ a sum for the row and column identically one and
avoid the simultaneous problem of weights to do so w/ too few constraints.

For the trivial case of the 2x2 example again
x=rand(2)
x =
0.5298 0.2091
0.6405 0.3798
y=x./repmat(sum(x),size(x,1),1)
y =
0.4527 0.3550
0.5473 0.6450
sum(y)
ans =
1 1
y(:,2)=1-y(:,1)
y =
0.4527 0.5473
0.5473 0.4527
sum(y)
ans =
1.0000 1.0000
sum(y,2)
ans =
1
1
Of course, depending on how large your array is and the size of values,
the two normalizations may not look very similar this way...

--
Steven_Lord
2012-11-27 18:15:20 UTC
Permalink
Post by zuzia
I'm working on a specific matrix and I need to normalize the sum of the
elements in all the rows and all the columns to be equal to 1. I have
managed to create the codes to do both separately but does anyone know how
to obtain both simultaneously?
thank you!!!
It's not generally possible. Two examples:

x1 = [1 1; 0 0];

There's no scaling factor by which you can multiple the second row to make
it sum to 1.

x2 = [1 1; 1 0];

No matter what scaling factors you apply to the second row or second column,
element (2, 2) won't change. Therefore elements (1, 2) and (2, 1) must each
remain at 1 to make the sum of the second row and second column be 1. But
that means the first row and the first column each sum to 2. You'd need to
scale by 0.5 to fix the first row/column sums but that messes up the sums of
the second row and column.

The mathematical term for such a matrix that you're trying to create (row
sums and column sums each equal to 1) is "doubly stochastic matrix"

http://en.wikipedia.org/wiki/Doubly_stochastic_matrix
--
Steve Lord
***@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com
Loading...