What's the best Matlab/Octave idiom, given idx
a vector of indices, to get the sorted vector of idx +/-1
?
I have an n x 7 data matrix, column 3 is an integer label, and I'm interested in viewing the neighborhood of discontinuities on it. Hence I get the corresponding indices:
idx = find(diff(data(:,3)) > 0)
5297
6275
6832
...
20187
Then if I want to view that neighborhood +/- 1 on my column (e.g. on the (mx2) matrix [idx-1; idx+1]
), I need to form the vector of idx-1, idx+1
either concatenated in-order, or resorted.
I found some clunky ways of doing this, what's the proper way?
(I tried all of the octave chapter on Rearranging Matrices)
% WAY 1: this works, but is ugly - a needless O(n) sort
sort([idx-1; idx+1])
% horzcat,vertcat,vec only stack it vertically
horzcat([idx-1; idx+1])
horzcat([idx-1; idx+1]')
% WAY 2?
%One of vec([idx-1; idx+1]) or vec([idx-1; idx+1]') should work? but doesn't, they always stack columnwise
horzcat([idx-1; idx+1]')
ans =
Columns 1 through ...
5297 6275 6832 ... 20187 5299 6277 6834 ... 20189
% TRY 3...
reshape([idx-1; idx+1], [36,1]) doesn't work either
You would expect there are only two ways to unstack a 2xm matrix, but ...