# Tag Info

21

I started with the Julia code you had and also got ~20 seconds, so I think my timings are similar to yours. Let me give a step by step breakdown on how to do this. To start, notice that if you are running code in the REPL that variables defined there are global. This incurs a good performance cost. There are two ways to deal with this: 1) Wrap it all in a ...

10

This cleanup code at the end is indicative of the problem with this program: clear i clear j clear k clear l clear m clear n clear o clear p Nobody is going to reverse-engineer your minified / obfuscated code to understand it. You will probably have a hard time understanding it yourself if you come back to it after a few weeks. The program is therefore ...

9

Your code appears to be very C-like with some C++. I'll just give some feedback in regards to that: Try not to use using namespace std. <string.h> is a C library; use <string> with C++. In C++, prefer std::size_t over size_t from C. For testing algorithms such as these, it's good to provide your main() to show how you're doing your testing. ...

9

You use very short variable names, so its hard to follow what your code is doing. For speed in numpy, you need to use vector operations. So instead of writing for ii in range(step): out[ii] = ii * x + b You should write: out = numpy.arange(step)*x + b Individually accessing the elements of numpy array is actually quite slow. You need to write your ...

8

For a full factorial experiment, you may want to avoid constructing the whole matrix just to get the factor levels for a single experiment, especially if you have a large number of factors and levels and you are using a language where loops are not very efficient. Here is how you can do it using mod. In a single factor experiment, you run the experiment ...

7

Vectorized approach: res = sum(abs(x(:).'.^(2:numel(x)+1))) Read more about vectorization techniques here. Thus, your function would look like this: function res = a3_funct(x) res = sum(abs(x(:).'.^(2:numel(x)+1))); return

7

Use a run-time profiler (test and measure, instead of or as well as trying to guess what's slow). Who knows, just from looking at it: the most expensive line of code might be something innocuous-looking like your push_back method calls. For a description of how to do this, see for example Profiling a mex-function. Code review The following apply to the ...

7

Vectorization If you look at all the calls to mean, you see that what you are calculating in each loop is a "Cumulative moving average", but in the oposite direction of what one usually would do. A cumulative moving average can be calculated quite simply using the cumulative sum, cumsum, and dividing by the number of elements. Since you're starting with ...

7

Your code looks fine and is vectorized! You could've written a single line logistic cost function, but I believe your approach is more readable. Good job! I don't think there is much more optimizations that you can do related to the basic form of logistic regression. A possible addition however is to add regularization. This helps for the scenario of ...

7

Regarding the vectorized code: permute can be considered a "Zero-cost operation". Logical operations are performed fastest using bsxfun. Arithmetic operations are fastest using bsxfun. Therefore, it's close to impossible to improve the performance of the vectorized code you've posted. Now, the question is: Is it correct? It's hard to tell without knowing ...

7

NOTE: The code in this answer is rather compact, in order to avoid a lot of scrolling. You might want to put in some additional spaces and line breaks. You know how to do this! Your code looks very nice and clean! But, this hurts my MATLAB eyes: for i = up_down_ratio_arr for j = traffic_load_arr for k = blocked_modes_ind_arr for l ...

7

Profiler I want to know where the bottle neck is... Use the profiler to find out, then you will know which specific lines to tackle. Note: the profiler itself will slow things down, so remember to turn it off when you are finished profiling. Optimising code: You can get rid of needless operations like + ran2 - ran2, won't make a big difference but cleans ...

6

You can define functions in MATLAB by creating files named like functionname.m with content like this: % the 'g' is the return value, 'sigmoid' is the function name and % 'z' is the only parameter function g = sigmoid(z) g = zeros(size(z)); g = 1./ (1 + exp(-z)); end One other general idea that can speed up MATLAB quite a lot is vectorization: When you ...

6

Avoid variable names that collide with built-in functions sum is a built-in function. Redefining it causes unconventional behavior: >> sum([1 2 3]) ans = 6 >> sum = 0.0 sum = 0 >> sum([1 2 3]) Index exceeds matrix dimensions. >> clear all >> sum([1 2 3]) ans = 6 Idiomatic MATLAB The whole point of ...

6

Rather than going through all of labels looking for the biggest in this line: count = histc(labels, 1:max(labels)) you can pick this number off directly with numel(labels): count = histc(labels, 1:numel(labels)) Alternatively, you can use accumarray: count = accumarray(labels,1); On this line in the loop if(~isempty(words{i}) && ~any(strcmp(...

6

A few notes: I'm confused about the design of your function. If you are only processing column vectors, why not just process a row vector instead? That's what I would expect when calling this function. my_function([1:2:20]') % how I have to call your function; convoluted and unexpected my_function(1:2:20) % how I expect to call your function; cleaner ...

6

You say getNeighboursByIndx takes 50% of the total time. That means that you should try to reduce either: The number of calls to getNeighboursByIndx The time it takes to run getNeighboursByIndx There's a lot of data I don't have, so trying to improve the number of calls to getNeighboursByIndx is hard. Improving the performance of the function itself ...

6

in your MATLAB code, some unnecessary steps can be skipped. Here's a modification of your doStep function that should improve performance quite a bit. Each modif is explained in the comments within the code. doStep2.m function file: function [ newtv ] = doStep2( tv, fitnessf, mutrate, stdev ) % Function to calculate new offspring trait values from parent ...

6

Few highlights: std::copy(std::begin(z), std::end(z), std::begin(zOut)); To copy input values to overwrite them is just a waste of time. You will overwrite them then you do not need to copy old values, use z instead of zOut inside loops. y.size() is possibly evaluated multiple times. It's a minor performance issue (and it may be inlined) but it depends on ...

5

First off: Indentation! If you have written the code and figure out afterwards that your indentation is wrong / missing / messy, Matlab makes it very easy for you! Ctrl+A followed by Ctrl+I. Simple as that, auto-indentation. Variable names. Choose descriptive, unambigous variable names. Remember that other people might need to read and understand your code! ...

5

Use consistent indentation. You switch from extremely verbose all lower case variable names like numoftrainingdata to single letter capitalized variable names like A. Make your variable names descriptive and no longer than necessary, and be consistent. Use consistent white space between operators. knn() doesn't need the second column of test_data, and the ...

5

There's a lot going on in this script, but I've tried to cover all of the important parts. Style and conventions: I think your coding style is very good. You're using nice a descriptive variable names - Good! Your variable names are consitent (camelCase) - Good! (Except pixels_per_mm) You're using spaces - Good! (Most of the places, [pathstr,name,ext]=...

5

Conventions, coding patterns and readability Overall, your code looks nice! I have never seen a code where you insert a line in the beginning to avoid the script being interpreted as a function. It works, but I wouldn't do it like that. The two functions you have are very simple. There is 1: no recursion. 2: no loops 3: no handling of invalid input. For ...

5

Regarding the rpois function, I think the preferred way of simulating a single value from a Poisson distribution with mean λ is using Distributions rand(Poisson(λ)) To fill out, a vector of Ints, with Poisson simulated values from a vector of means, p, you can use the mutating version of map. In v0.4 it is best to write a helper function r1(λ) = rand(...

5

I'm assuming the data in variable A is calculated somehow in MATLAB, and that you have a matrix A that you want on a tab-delimited format, with headers. If that's the case, then writetable is probably your best bet (as you have figured out). The expressions below are very hard to scale, since 100 rows of data results in 100 rows of code. C(3,:) = num2cell(...

5

You felt it right, there's a lot of unnecessary copy/paste in the first part. Your 5 data structures are similar, and you create a lot of variables to perform the same processing 5 times. I did a quick factorisation which brings back the code to a reasonable 12 lines (I could not test, obviously, but that seems OK): result_list = [] for name in ["AWA","...

5

I can comment on multiple things here. But you ask about making the code look cleaner, so I'll start with that Indenting Be consistent with indenting. Indents show the structure of your code. Inconsistent indenting makes it hard to follow the structure. Consistent spacing around operators also helps readability. Variable names It's common in MATLAB code ...

5

Efficiency Things that jump out. std::vector<ABPair*> validPathsThread; An array of pointer looks odd. Especially since ABPair is simply a pair of integers. I notice in the code you are using malloc() to allocate them which itself is a very expensive operation. I would just remove the pointer here: Then this code: ABPair* temp = ...

4

For new users coming to this page, Armadillo has come a long way since 2012. All of these functions have native Armadillo implementations. Armadillo has had element-wise functions since inception I think (someone please correct me): log(A), log2(A), and log10(A): using namespace arma; // Generate a matrix with the elements set to random floating point ...

4

Why don't you read the index from http://www.rbsp-ect.lanl.gov/data_pub/rbspa/MagEphem/def/ and use that to decide which files you want to download? Also, try to give multiple files (eg. 20) to each curl call: this will enable curl to reuse the same connection for those files. Please also note that Matlab is not the right tool for this job. For example, ...

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