# Tag Info

## Hot answers tagged matlab

4

Before even discussing any vectorization, there are gross inefficiencies in the code you provided. Just by employing basic techniques, I got 21-fold speedup: n = 1000000; y = zeros(1,n); tProbs = [0.1 0.5; 0.9 0.5]; iProbs = sum(tProbs^100000,2); iProbsSqr = iProbs.^2; V0 = [1;1]; for i=1:n V0 = tProbs*V0; y(i) = sqrt(dot(V0.^2,iProbsSqr)); end If the ...

3

(This answer is valid for both MATLAB and Octave, even though I mainly refer to MATLAB) There are two beasts to slay; but let's first understand the underlying data structure. MATLAB and Octave store sparse matrices in the coordinate (COO) format, i.e. a sparse matrix S is a collection of three arrays of the length equal to the number of nonzero entries of S:...

3

Probably not what OP was waiting for, but I think it could be pretty instructive and useful. FEM codes use a much different approach to build the so-called stiffness matrix. In practice, they loop over elements and compute for each element small matrices (in your case if you use linear elements 3by3) which are distributed to the right entries of the global ...

2

This is a (FDM) supplement to VoB's answer. You could write an extremely vectorized and optimized solver for your problem, but that is not a good first idea. Writing a for loop is easier, and once you identify the opportunities for optimization, you can implement them. Here is how I would go about it (in pseudo-code): Assume $0\leq i\leq n$ and $0\leq j\leq ... 1 First of all, I want to ask if you are sure that when you are saying$n=10000$you mean$10^4$points in one dimension? Because that would mean$10^8$points over the domain, and that is too many points to solve such a simple problem. You would hit the minimum error before$10^8\$ points, and you wouldn't gain anything for the extra work you do. Secondly, you ...

1

They only deal with the 2D case, but I can point you to the work of some of my colleagues on https://doi.org/10.1137/17M1157155 . Using the off-diagonal low-rank structure in a recursive fashion, they can reach quasi-linear cost for the 2D case.

1

I think the main use of it is abstraction and the advantages that come with it: Applying a function to each element of an array comes up in many situations. Wrapping this pattern into a function raises the level of abstraction: you do not need to think in loops, your intent is probably clearer. If you are familiar with Python, comprehensions do this, as ...

Only top voted, non community-wiki answers of a minimum length are eligible