![]() Remember the rooting example from our previous lecture? We wrote three versions of it and our last version was 200 times faster than our first naive attempt. ![]() #Matlab vectorize code#As you will see, it can result in dramatic speedups of your program, and in many cases, you'll get code that's less prone to error and easier to debug in the bargain. If you come from that tradition and even if you don't, I recommend strongly that you make a conscious effort anytime you're implementing a computationally intensive algorithm to try to avoid explicit loops, and think of ways in which you can solve your problem with vector commands instead. Even when they have the power of implicit looping at their fingertips, they have a strong tendency to stick with explicit loops because that's what they're used to. But pity to people who, like I did, come from a traditional programming language like C, C++ or Java that lack the built-in support for vectorization that MATLAB has. If you're new to programming and MATLAB is your first language, then you are in good shape. In every case of vectorization, at least one explicit loop is avoided, and you should strive to avoid explicit loops if there's any way to accomplish the same thing with vectorization. The translation of code from a version that uses explicit looping and to one that uses implicit looping via vector command is called vectorization. A function or an operator that performs matrix multiplication or any function that operates on an entire vector or an entire array is said to be a vector command. ![]() As a result, many slow MATLAB functions can be changed into fast functions by replacing explicit loops with operations or functions that operate on entire arrays via implicit loops. Furthermore, it's implemented by highly optimized low-level libraries of functions that are extremely fast. Looping still happens under the hood, but you can avoid all the programming time required to make it happen. What all this implicit looping means is that a simple MATLAB command like adding, subtracting, multiplying, or even dividing two matrices does not require even one explicit for-loop or a while loop, or include even one index variable. Implicit looping is everywhere in Matlab with all its other arithmetic and logical operators and the hundreds of built-in functions that handle array inputs and outputs. But matrix multiplication is just the beginning. The most obvious example of implicit looping in MATLAB is the matrix multiplication operator, in which one asterisk takes the place of three nested for loops, three index variables, eight indices, and scalar multiplication and addition operators that are required in our MATMOL function, for example. Moler's decision some 40 years ago to incorporate those operators directly in MATLAB forced him to incorporate the same implicit looping in the language itself, and it has had a huge impact on numerical programming. The notation of numerical computing included operations like matrix multiplication and matrix transposition each symbolized by single operator, with all the looping and indexing required to carry out those operations implied but not written out on the page. He was using the notation of that field everyday when it occurred to him that there should be a computer language that incorporated that notation. The inventor, Cleve Moler, was and still is an expert in the field of numerical computation. Decisions on implementation are especially important in MATLAB, which was invented expressly to work with matrices and arrays. #Matlab vectorize how to#Deciding which ones to use and how to combine them while coding a particular algorithm can have a profound effect on the speed of your program. All programming languages come with built-in operators and built-in functions. The reason for that is that there are many different ways to implement the same algorithm, an implementation can make a big difference. But what if you've settled on what you believe to be the ideal overall algorithm for your problem and yet your implementation is still too slow for your purposes, is there anything more you can do to improve it? Many times, the answer is yes. ![]() By now, I hope we have convinced you of the importance of the role of algorithms in programming and specifically in the efficiency of your programs. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |