Avoiding loops in MatLab code (barycentric weights)

After learning basic programming in Java, I found that the hardest part of moving to MatLab for my current course in the algorithm is to avoid loops. I know that there are many clever ways to vectorize operations in MatLab, but my mind is so stuck in loop thinking that it’s hard for me to intuitively see how I can vectorize code. Once they show me how to do this, it makes sense to me, but I just don't see it easily. I currently have the following code for finding the barycentric weights used in Lagrangian interpolation:

function w = barycentric_weights(x); % The function is used to find the weights of the % barycentric formula based on a given grid as input. n = length(x); w = zeros(1,n); % Calculating the weights for i = 1:n prod = 1; for j = 1:n if i ~= j prod = prod*(x(i) - x(j)); end end w(i) = prod; end w = 1./w; 

I'm sure there should be a smarter way in MatLab to do this, but I just can't think about it. If anyone has any advice, I will be very grateful :). And the only way that I will ever recognize all the vectorization tricks in MatLab is to see how they are used in various scenarios like the one above.

+4
source share
3 answers

I see the attractiveness of vectorization, but I often ask myself how much time it actually saves when I return to the code in a month and have to decrypt everything that repmat gibberish. I think your current code is clean and clear, and I would not mess with it if performance is not very important. But answering your question here is my best efforts:

 function w = barycentric_weights_vectorized(x) n = length(x); w = 1./prod(eye(n) + repmat(x,n,1) - repmat(x',1,n),1); end 

Hope this helps!

And I assume that x is a row vector.

+2
source

In matlab, you need to be creative in order to avoid a loop:

 [X,Y] =meshgrid(x,x) Z = X - Y w =1./prod(Z+eye(length(x))) 
+3
source

Christian, there are many ways to vectorize code. You already got two. (And I agree with shakinfree: you should always consider 1) how much time it takes to work in a non-vectorized form (so that you will have an idea of ​​how much time you can save by vectorizing); 2) how much time you will need for vectorization (so that you will have a better idea of ​​whether this time is worth you; 3) how many times will you call it (again: is it worth it); and 3) readability. As shakinfree suggests, you don’t want to return to your code after a year and scratch your head about what you have implemented. At the very least, make sure you comment well.

But at the meta level, when you decide that you need to improve execution performance by vectoring, first start with a small (3x1?) Array and make sure you understand exactly what happens for each iteration. Then spend some time reading this document and follow the appropriate links:

http://www.mathworks.com/help/releases/R2012b/symbolic/code-performance.html

This will help you determine when and how to vectorize.

Happy MATLABbing!

Brett

+3
source

Source: https://habr.com/ru/post/1442183/


All Articles