The meaning of colon operator in MATLAB

Actually a:b generates a vector. You could use it as index only because the (…) accepts a list also, e.g. octave-3.0.3:10> a = [1,4,7] a = 1 4 7 octave-3.0.3:11> b = [1,4,9,16,25,36,49] b = 1 4 9 16 25 36 49 octave-3.0.3:12> b(a) # gets [b(1), b(4), b(7)] ans = 1 16 49 Now, … Read more

Stable accumarray in MATLAB

We can use sortrows as a preprocessing step to sort the indices and corresponding values first, as its documentation states: SORTROWS uses a stable version of quicksort. As the subscripts in subs should be sorted with respect to their linear indices, we need to sort them in reverse lexicographic order. This can be achieved by … Read more