MATLAB: Could someone help reducing the ‘for’ loops in the code? Please? :)

bipartitenetworks

Since I'm new to programming I'm unable to figure out myself on how to rewrite this code to reduce it's runtime. I have written this program as my first homework but did not succeed 🙁 But I would like to complete it now and the program i wroke has a lot of "for" loops which is taking a lot of time to run the program. The program is about projecting two sets U and V in a bipartite graph and I have written the program separately both of them. (Where set U= actors and V= movies). Could you help me with a better choice/alternative for this code which reduces the runtime of the program?
% Projections of actors (set X)
adj_Xw = zeros(1049,1049);
for i=1:1049 % columns

for a=1:1049 % rows

if (i>a)
adj_Xw(a,i) = adj_Xw(i,a);
elseif (i==a)
adj_Xw(a,i) = 0;
else
for b=1:nm
if (A(i,b)==1 && A(a,b)==1)
adj_Xw(a,i) = adj_Xw(a,i) + 1;
end
end
end
end
end
adj_X = adj_Xw;
for i=1:1100401 % Plain version of projection(X)
if adj_X(i)>0
adj_X(i)=1;
end
end
X = graph(adj_X);
Xw = graph(adj_Xw);
save ('variables');
%Projection of movies (set Y)
adj_Yw = zeros(345,345);
for i=1:345 % columns
for a=1:345 % rows
if (i>a)
adj_Yw(a,i) = adj_Yw(i,a);
elseif (i==a)
adj_Yw(a,i) = 0;
else
for b=1:na
if (G(i+1049,b)==1 & G(a+1049,b)==1)
adj_Yw(a,i) = adj_Yw(a,i) + 1;
end
end;
end;
end;
end;
adj_Y = adj_Yw;
for i=1:119025 % Plain version of projection(Y)
if adj_Y(i)>0
adj_Y(i)=1;
end
end;
Y = graph(adj_Y);
Yw = graph(adj_Yw);

Best Answer

If you are new to programming, I strongly encourage you to go through MATLAB Onramp. It's interactive, and will give you a good foundation to start your programming.
For loops are often removed by taking advantage of matrix operations. You need to understand those before simplified code will make sense.