on-internet
← Community

How to simplify my double loop using MATLAB vectorization?

Stack Overflow ·

My code is: N = 500; % D_1=ones(N,N);% D_2=ones(N,N);% For simplicity. B=zeros(N,N); for i = 1:N for j = i+1:N basis_vector = zeros(N, 1); basis_vector(i) = 1; basis_vector(j) = -1; if D_2(i,j)>0 w = D_1(i,j) / D_2(i,j); else w = 0; end B = B + w .* basis_vector * basis_vector'; end end How can I simplify my code? Because N is very large, my code takes a long time to finish.

Comments

Sign in or sign up to comment and upvote.

No comments yet.