![]() Original answer (about how to built your own fast functions working on arrays) The actual work accessing all array elements was done by sort, which is a built-in (i.e. In other words, the code you show only needs to access the median values by their correct index in the now-sorted multi-dimensional array x (which doesn't take much time). The code snippet you give doesn't really contain the relevant line of code which does the main work, namely % Sort along given dimension This is why MATLAB internally does some loops really well - often by coding them as a compiled function. MATLAB is often used for prototyping / one-off calculations, so it makes sense to save time for the person coding, and giving up some of C's flexibility. Once you get used to it, it is actually really nice to just have to type one line and have MATLAB do the full 4D looping stuff itself without having to worry about it. MATLAB is optimized to work well with arrays. Update 2 (to address your updated question) It is really hard for me to understand how can an industry leading language have such an extreme performance difference based on the programming style, but I see the points mentioned in the answers below. ![]() It means that the built in way for sum is 160 times faster than the nested loops. I re-did the tests with mean instead and the results are even more crazy: I realized that using median was not a good example as it is a complicated function itself requiring sorting of the array or other neat tricks. I don't understand how could it be 7x times faster and also, that why is it so complicated. Y(ixMedians) = meanof(x(absIndex),y(ixMedians)) įor consIndex = (nCompare-1)*numConseq:nCompare*numConseq-1Ĭould you explain to me that why is this code so effective compared to the simple nested loops? It has nested loops just like the other function. % Nested FOR loop tracks down medians by their indices.įor consIndex = half*numConseq:(half+1)*numConseq-1įor consIndex = (half-1)*numConseq:half*numConseq-1 Y = repmat(x(1),numMedians,1) % Preallocate appropriate type Increment = cumSize(dim) % Gap between runs of indices NumConseq = cumSize(dim - 1) % Number of consecutive indices Total = cumSize(end) % Equivalent to NUMEL(x) Use linear indices to extract medians, then reshape at I found the code for calculating median in higher dimensions % In all other cases, use linear indexing to determine exact location How do you write a function my_median_nD which acts like MATLAB's median, by taking an n-dimensional array and a "working dimension" parameter? My question is that how do you write a function yourself which works as efficiently as one which has this dimension range implemented?įor example you have a function my_median_1D which only works on a 1-D vector and returns a number. In that example instead of looping through all dimensions of a 4 dimensional array and calculating median for each vector, it was much cleaner and faster to just call median(stack, n) where n meant the working dimension of the median function.īut median is just a very easy example and I was just lucky that it had this dimension parameter implemented. 4 nested loops made a 7x times difference in running time. In my previous question: Nested for loops extremely slow in MATLAB (preallocated) I realized that using a vectorized function vs. In numpy, the solution is usually adding paddings.I am just learning MATLAB and I find it hard to understand the performance factors of loops vs vectorized functions. This poses some great challenges when we want to write a general purpose code to retrieve sparse matrices based on the topological structures. The number of indices stored in each element is of variable lengths. As it requires a polytopal data structure, the biggest difference with traditional finite element is: Recently I am learning to code “virtual element method” for Long Chen’s $i$FEM. Operates in 4k by 4k matrices hundreds even thousands of times faster than direct implementations in compiled languages like C/C++ and Java. Nevertheless, MATLAB is highly optimized in vectorized array and matrix operations using the LAPACK/BLAS backend, and as an interpreted/scripting language, MATLAB Looping through a large array is usually a nightmare, even more so if we add if/then within, and/or for sparse matrices. Element-wise matrix vector multiplicationĪs is known, MATLAB is notoriously slow in executing for loops.Vectorization tricks for cell arrays in MATLAB ![]()
0 Comments
Leave a Reply. |