1

I have the above loop running on the above variables:

  • A is a 2d array of size mxn.
  • mask is a 1d logical array of size 1xn
  • results is a 1d array of size 1xn
  • B is a vector of the form mx1
  • C is a mxm matrix, m is the same as the above.

Edit: expanded foo(x) into the function.

here is the code:

temp = (B.'*C*B);    
for k = 1:n
    x = A(:,k);
    if(mask(k) == 1)
        result(k) = (B.'*C*x)^2 / (temp*(x.'*C*x)); %returns scalar
    end
end

take note, I am already successfully using the above code as a parfor loop instead of for. I was hoping you would be able to suggest some way to use meshgrid or the sort to yield better performance improvement. I don't think I have RAM problems so a solution can also be expensive memory wise.

Many thanks.

7
  • Does your foo admit a matrix input, or only columns? Commented Oct 29, 2013 at 15:16
  • Preallocate result. Change if(mask(k) == 1) to if mask(k). You won't get much gain from that, though. Commented Oct 29, 2013 at 15:18
  • Hi, I don't think there should be any problem with matrix inputs. Commented Oct 29, 2013 at 15:20
  • why won't you give a minimal working foo example? Commented Oct 29, 2013 at 15:40
  • Also, with the current size definitions of your variables, you should use A(k,:) in the code and not A(:,k) ... Commented Oct 29, 2013 at 15:41

2 Answers 2

1

try this:

 result=(B.'*C*A).^2./diag(temp*(A.'*C*A))'.*mask;

This vectorization via matrix multiplication will also make sure that result is a 1xn vector. In the code you provided there can be a case where the last elements in mask are zeros, in this case your code will truncate result to a smaller length, whereas, in the answer it'll keep these elements zero.

Sign up to request clarification or add additional context in comments.

Comments

1

If your foo admits matrix input, you could do:

result = zeros(1,n); % preallocate result with zeros
mask = logical(mask); % make mask logical type
result(mask) = foo(A(mask),:); % compute foo for all selected columns

5 Comments

why do you preallocate with NaN and not zeros?
Because I didn't know which result you wanted for the columns not in mask. But yes, 0 is implicit in your code. I have corrected my answer
its currently failing. i guess the function foo isn't that suitable for matrices. i'll look into it. is there anything that can be done without putting matrices into foo?
Why is there an extra logical in the last line? mask is already logical.
@user2324712 If you need to feed foo with each column separately, a for loop is probably the best you can do. And it can be parallelized. You could perhaps save some time replacing for k=1:n by for k=find(mask) and removing the if. At the end you would have to "expand" result, along the lines of result(logical(mask))=result;

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.