1

For the following code I want to optimize it using the pattern introduced in this solution. However, the problem is how to deal with referring to three nested loops in a single statement. Moreover, the condition is far different from that post.

hint: W and S are NxN sparse double matrices.

    for i=1:N
    for j=1:N
        for k=1:N
            if W(j,k)~=0       
                temp(k)=S(i,j)-S(i,k); 
            end
        end
              sum_temp=max(temp)+sum_temp;
              temp=0;
    end
    B(i,i)=sum_temp;
    sum_temp=0;
end
3
  • The inner two loops are very similar to the code piece I address in the last section of this answer on Code Review. You should be able to follow that recipe to turn those two loops into a single statement. Commented Feb 20, 2020 at 15:57
  • I obviously have no clue what your code does, but I am surprised that temp is never reset to 0. Before we start trying to optimize this, could you confirm that this is correct? Commented Feb 20, 2020 at 19:11
  • @Daniel, the code has been edited. Commented Feb 20, 2020 at 21:06

1 Answer 1

3

In this situation I would opt against fully vectorizing your solution. Calculating S(i,j)-S(i,k) for each combination would mean an intermediate result of size [N,N,N]. Instead I went through your code and eliminated as much iteration as possible without increasing the memory consumption. Step by step so you can understand how I ended up there.

N=30;
S=rand(N,N);
W=rand(N,N)<.1;
sum_temp=0;
temp=0;
%Your original code for reference
for i=1:N
    for j=1:N
        for k=1:N
            if W(j,k)~=0
                temp(k)=S(i,j)-S(i,k);
            end
        end
        sum_temp=max(temp)+sum_temp;
        temp=0;
    end
    B(i,i)=sum_temp;n
    sum_temp=0;
end
B_orig=B;
%1) you only want the max, no need to make temp a vector
for i=1:N
    sum_temp=0;
    for j=1:N
        temp=0;
        for k=1:N
            if W(j,k)~=0
                temp=max(temp,S(i,j)-S(i,k));
            end
        end
        sum_temp=temp+sum_temp;
    end
    B(i,i)=sum_temp;
end
assert(all(all(B==B_orig)))
%2) eliminate the outer loop
sum_temp=zeros(N,1);
for j=1:N
    temp=zeros(N,1);
    for k=1:N
        if W(j,k)~=0
            temp=max(temp,S(:,j)-S(:,k));
        end
    end
    sum_temp=temp+sum_temp;
end
B=diag(sum_temp);
assert(all(all(B==B_orig)))

%3) combine the inner loop with the condition
sum_temp=zeros(N,1);
for j=1:N
    temp=zeros(N,1);
    for k=find(W(j,:))
        temp=max(temp,S(:,j)-S(:,k));
    end
    sum_temp=temp+sum_temp;
end
B=diag(sum_temp);
assert(all(all(B==B_orig)))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.