File Exchange

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

version 1.3 (3.39 KB) by

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

Updated

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.

ouyoung kwon

Amir

### Amir (view profile)

It works fine on well explained example vectors but not on double vector type. Any comment on that?

Nicolas

### Nicolas (view profile)

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.

Val

### Val (view profile)

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.

Nicolas

### Nicolas (view profile)

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.

William Sutton

### William Sutton (view profile)

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

 16 Sep 2013 1.3 13. Sept. 2013: Included inf and i/j as possible arguments to remove infinity and complex elements, too. 28 May 2013 1.1 corrected 'end' to 'last', as end was obviously interfering with standard instruction.
##### MATLAB Release
MATLAB 7.14 (R2012a)