View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

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

5.0 | 3 ratings Rate this file 13 Downloads (last 30 days) File Size: 3.39 KB File ID: #41941 Version: 1.3

Snip.m Snip elements out of vectors/matrices


Nicolas (view profile)


24 May 2013 (Updated )

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

| Watch this File

File Information

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:


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.


This file inspired Circles Overlap Remover.

Required Products MATLAB
MATLAB release MATLAB 7.14 (R2012a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (7)
13 Apr 2016 ouyoung kwon

26 Aug 2014 Amir

Amir (view profile)

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

Comment only
26 Sep 2013 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.

Comment only
21 Sep 2013 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.

Comment only
17 Sep 2013 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.

Comment only
04 Sep 2013 William Sutton

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

26 Jun 2013 Elad

28 May 2013 1.1

corrected 'end' to 'last', as end was obviously interfering with standard instruction.

16 Sep 2013 1.3

13. Sept. 2013: Included inf and i/j as possible arguments to remove infinity and complex elements, too.

Contact us