0

In MATLAB, I execute something like this:

z = diag(c)*D*x;
idxN = z<0; idxP = z>=0; % logical indexing
y1 = sum(-z(idxN))+sum(arrayfun(@(x) log(exp(x)+1),z(idxN))); 

I'm trying to formulate a Python one:

z = np.diagflat(c).dot(D).dot(x)
idxN = z<0, idxP = z>=0
y1 = np.sum(-z[idxN])+np.sum(math.log(np.exp(x)+1),z[idxN]); 

The above snippet doesn't work as I'm guessing I've to vectorize somewhere?

Thanks for your inputs.

1
  • 2
    arrayfun isn't vectorization, it's shorthand for a loop. Just use a loop, or rewrite your code to use vectorization. Commented Jun 20, 2018 at 8:09

1 Answer 1

1

arrayfun is a one-line wrapper for a loop in MATLAB. In Python, you can do it like this:

y1 = np.sum(-z[idxN]) + np.sum([math.log(np.exp(x)+1) for x in z[idxN]])
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.