MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
Asked by Weijian on 12 May 2013

Hi,

I have a sparse matrix L of zeros and ones and I want to change the non-zero entries to some fractions. This is my code:

beta = 0.5 n = size(L,1); %set n = dim(L) rsv=ones(1,n)*L; %row sum vector of L H =L; for i=1:n for j=1:n if H(i,j) == 1 H(i,j)=1/((1-beta)*rsv(i)+ beta*rsv(j)); end end end

I found this code works for the matrix of size 5 but does not work for the matrix of size 100 (all entries stay unchanged).

Do you know why and how to change the code?

Many thanks,

Weijian

*No products are associated with this question.*

Answer by Matt J on 12 May 2013

Accepted answer

If L has a single 1 on all columns then rsv(i)=1 for all i. If that happens then

1/((1-beta)*rsv(i)+ beta*rsv(j))

will equal 1 for all i and j.

## 5 Comments

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/75478#comment_148469

What is it that doesn't work? It looks good to me? Do you get an error?

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/75478#comment_148470

rsv=ones(1,n)*L;It should be more efficient to implement this as

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/75478#comment_148471

It should also be more efficient to avoid the for-loops as follows

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/75478#comment_148520

Thanks for your suggestion. Your code solves the problem :)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/75478#comment_148523

It is not really clear why this should have made a difference. It is equivalent to your original code, if L is type double. I can only suppose that L was logical and that my other Answer below accounts for the difference.