Code covered by the BSD License

### Highlights from Snip.m Snip elements out of vectors/matrices

5.0

5.0 | 2 ratings Rate this file 54 Downloads (last 30 days) File Size: 3.39 KB File ID: #41941

# Snip.m Snip elements out of vectors/matrices

by

24 May 2013 (Updated )

Solves problems s.a. 'remove all nan' from a vector, 'get rid of 14th row of matrix' with low effort

File Information
Description

I often end up having to remove certain elements from a vector, or I don't need certain lines from my data and similar problems. While matlab has great matrix manipulation capability, I find myself continuously forgetting which [(:,)):,) combination would do the trick nicely and then proceed to Google. Therefore, my first attempt at creating something useful turns matlab logic upside down: Imagine your vector is on a strip of paper, and you cut out all the unwanted nan's. Then paste the rest together. Same with matrices. Also works with specifying the rows and columns to get rid of etc.

This aim is to remove large accumulations of logical index descriptions (bracket-clusters) and provide an easy command instead:

snip(input,snipthis)

While it is not perfect (written in a flurry), it worked for me so far - so please provide feedback to make it better. It should work with everything necessary, except self-defined data-structures.

Acknowledgements

This file inspired Circles Overlap Remover.

Required Products MATLAB
MATLAB release MATLAB 7.14 (R2012a)
26 Sep 2013

Hi Val,
Thank you for that idea. I mused over it a bit and decided not to do that. First, because it would overburden the function with something not many people would use - I reckon. And second, because the way it is implemented your idea would require substantial rewriting for the necessary bookkeeping in snip.m.

For your application, I recommend to stack all your data like this:
A = [0 0 0; 1 2 3; 4 5 6];
B = [1 2 3; 4 5 6; 7 8 9];
C(:,:,1) = A; C(:,:,2) = B;
And now find the appropriate case in snip.m and implement that a 'slice' is cut out from your 'stack' by eliminating all those columns/rows under/above e.g. where A has the [0 0 0]. This should easily be possible in a similar way as in snip.m, when you use only the A 'sheet' in the 'stack' as the steering mechanism for the algorithm.

21 Sep 2013

This is great! I have had similar problems, so this is a great solution.

One additional functionality would be appreciated, which is that the function can optionally output the indices of the old matrix that are kept after snipping. This way, any other matrices that are the same size as the old matrix can be pared down in the appropriately matched rows and columns.

17 Sep 2013

Hi William, thx for the feedback. I put in the option to give inf and i or j as arguments for removing infinity and complex items just like the others. Wish I could find how to answer your comment directly, though.

04 Sep 2013

Seems to work quite well. Any advice on getting it to remove Inf?

26 Jun 2013