From: Paul on
Hi,

I'm trying to emulate a Page Rank matrix in which all the columns must add up to one (not "be normalized")

ie

function [A] = MatrixGenerator(Size)
A = sprand(Size,Size,0.5);
A(A ~= 0) = 1;
end

Is the code I've got to create a random nxn matrix with random 1s and 0s.
I wish to add up all the elements in a column (effectively) and divide the COLUMN (not the matrix) by the number of 1s for EACH column.

So
1 0 0 0 0
0 1 0 1 0
1 0 1 1 0
0 0 0 0 1
1 1 0 0 0

would (hopefully!) become

1/3 0 0 0 0
0 1/2 0 1/2 0
1/3 0 1 1/2 0
0 0 0 0 1
1/3 1/2 0 0 0

Thank you in advance for any sort of hints at where to start!
From: Paul on
I've updated my code to this...

function [A] = MatrixGenerator(Size)
A = sprand(Size,Size,0.5);
A(A ~= 0) = 1;
[~,col]=find(A);
columnoperation = 1;
while columnoperation <= Size
numberofelements = sum(col==columnoperation);
columnoperation = columnoperation +1;
end
end


Trying to work out how to just divide a certain column by the numberofelements now...
From: Bruno Luong on
A=spones(sprand(10,10,0.5));
% Normalize
B=A*spdiags(1./sum(A,1).',0,10,10);
% Check
sum(B,1)

% Bruno
From: Jos (10584) on
"Paul " <paul(a)pangeaenterprisesltd.com> wrote in message <htlclt$bd8$1(a)fred.mathworks.com>...
> Hi,
>
> I'm trying to emulate a Page Rank matrix in which all the columns must add up to one (not "be normalized")
>
> ie
>
> function [A] = MatrixGenerator(Size)
> A = sprand(Size,Size,0.5);
> A(A ~= 0) = 1;
> end
>
> Is the code I've got to create a random nxn matrix with random 1s and 0s.
> I wish to add up all the elements in a column (effectively) and divide the COLUMN (not the matrix) by the number of 1s for EACH column.
>
> So
> 1 0 0 0 0
> 0 1 0 1 0
> 1 0 1 1 0
> 0 0 0 0 1
> 1 1 0 0 0
>
> would (hopefully!) become
>
> 1/3 0 0 0 0
> 0 1/2 0 1/2 0
> 1/3 0 1 1/2 0
> 0 0 0 0 1
> 1/3 1/2 0 0 0
>
> Thank you in advance for any sort of hints at where to start!

Take a look at BSXFUN

% data
A = sprand(5,5,0.5)>0 ;
% engine
nA = sum(A) ;
nA(nA==0) = 1 ; % avoid NaNs in B
B = bsxfun(@rdivide, A, nA) ;
% result
full(A)
full(nA)
full(B)
sum(full(B))

hth
Jos
From: Paul on
"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <htlfc4$6u5$1(a)fred.mathworks.com>...
> A=spones(sprand(10,10,0.5));
> % Normalize
> B=A*spdiags(1./sum(A,1).',0,10,10);
> % Check
> sum(B,1)
>
> % Bruno

Haha...
Ok,

I made mine work:

function [A] = MatrixGenerator(Size)
A = sprand(Size,Size,0.5);
A(A ~= 0) = 1;
[~,col]=find(A);
columnoperation = 1;
while columnoperation <= Size
numberofelements = sum(col==columnoperation);
if numberofelements == 0
numberofelements = 1;
end
columnworkingon = A(:,columnoperation);
columnworkingon = numberofelements.\columnworkingon;
A(:,columnoperation) = columnworkingon;
columnoperation = columnoperation +1;
end
end

But yours is quite obviously a lot better! Thanks
 | 
Pages: 1
Prev: Out of Memory Error in fedora 11
Next: streamlines