Code covered by the BSD License  

Highlights from
Vector to colon notation

4.0

4.0 | 1 rating Rate this file 3 Downloads (last 30 days) File Size: 2.71 KB File ID: #22076

Vector to colon notation

by Javier Lopez-Calderon

 

13 Nov 2008 (Updated 18 Mar 2011)

Converts a vector into a string with its MATLAB colon notation (single resolution).

| Watch this File

File Information
Description

 Usage: strvec = vect2colon(vec, options)

 Converts a vector into a string using MATLAB colon notation (single resolution).

 Options are:

 'Delimiter' - 'on','yes','off','no' {default on} : Including square brackets []
                    (or curly brackets {} when output's dimensions are not consistent)
                    'auto' by default.
 'Sort' - 'on','yes','off','no' {default off} : Sort elements in ascending order
                    
 'Class' - MATLAB classes

 'Repeat' - 'on','yes','off','no' {default on} : Keep repeated elements

 MATLAB colon notation is a compact way to refer to ranges of matrix elements.
 It is often used in copy operations and in the creation of vectors and matrices.
 Colon notation can be used to create a vector as follows
                    x = xbegin:dx:xend
                    or
                    x2 = xbegin:xend
 where xbegin and xend are the range of values covered by elements of the x
 vector, and dx is the (optional) increment. If dx is omitted a value of 1
 (unit increment) is used. The numbers xbegin, dx, and xend need not be
 integers.

 Example 1:
 >> x = [ 50 1000 1100 1200 2 3 4 5 6 10 20 30 40]
 >> vect2colon(x)

 ans =

 [ 50 1000:100:1200 2:6 10:10:40]

 or

 >> vect2colon(x, 'Sort', 'on')

 ans =

 [ 2:6 10:10:50 1000:100:1200]

 Example 2:
 >> x = [ 2 3 4 5 6 10 10.1 10.2 10.3 10.4 1000 1100 1200]
 >> s = vect2colon(x)

 s =

 [ 2:6 10:0.1:10.4 1000:100:1200 ]

 Example 3:
 >> x = [ 2 3 4 5 6 10 1000 1100 1200 10.1 10.2 10.3 10.4]
 >> s = vect2colon(x, 'Delimiter', 'no')

 s =

  2:6 10 1000:100:1200 10.1:0.1:10.4

 See also: mat2colon, eval, str2num

 Feedback is appreciated.

MATLAB release MATLAB 7.4 (R2007a)
Tags for This File  
Everyone's Tags
colon, display, eval, mat2colon, matrix indexing, single resolution(2), string, vector, vectorization
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (7)
20 Mar 2011 Bruno Luong

I rather like the idea, but I was disappointed mainly by the fact that the function returns a string instead of number.

- EVAL would be necessary to build back the original array. I would prefer COLON rather than EVAL.
- The precision get lost in the way

vect2colon([0:0.1234567890123456:1],'Class','double')

ans =

0:0.12346:0.98765

I would prefer empty input returns '[]' for the following reason:

>> a=eval(vect2colon([]))
??? Error: Expression or statement is incomplete or incorrect.

The engine could also be better, for example this command takes very long to complete:

>> vect2colon([0:1e-7:1])

and the casting dvec to SINGLE is a clear sign of the limitation of the transformation.

19 Mar 2011 Javier Lopez-Calderon

Please, see also mat2colon.m here http://www.mathworks.com/matlabcentral/fileexchange/30797-mat2colon

26 Jan 2010 Javier Lopez-Calderon

Thanks Jan for remembering and taking into account my vect2colon function. I also apologize for my long absence...many things have delayed me this time. I'm currently using an enhanced version of vect2colon, so I'll be back with this updating. Nonetheless, Sven Bossuyt function seems to be working well.
Greetings.

26 Jan 2010 Jan Simon

See also: http://www.mathworks.com/matlabcentral/fileexchange/26453

23 Oct 2009 Jonas

Exactly what I needed for my GUI.

Note: It would be useful if there was a check for whether the input is numeric. The code works if you feed the output back as input, but it doesn't give very good results. E.g. '1:999' becomes '1:9'

12 Dec 2008 Javier Lopez-Calderon

Hi Jos, thanks for take a look on vect2colon, and for your sharp observation.

Indeed, if you try this in Matlab:

>> a = [.1 .2 .3 .4 .5 .6 .7 .8 .9 1];
>> b = 0.1:0.1:1;
>> a==b

ans =

1 1 0 1 1 1 1 1 1 1

they don't match either. Now, for single precision we have

>> a = single([.1 .2 .3 .4 .5 .6 .7 .8 .9 1]);
>> b = single(0.1:0.1:1);
>> a==b

ans =

1 1 1 1 1 1 1 1 1 1

now they do.

So, I'll work on vect2colon in order to assure this last behavior.

Regarding to the exemplary situation: Actually, at the beginning, I wrote vect2colon with the purpose to work with integers, for matrix indexing and vectorization.

For instance, I have been working with large files from electrophysiological recordings using GUIs. So, when you have a GUI with several windows, and each window with a tiny room for writing several number of channels, or bins, or subjects, etc, vect2colon has been very useful for my work. I mean, if a write or delete any number (in any order) in a window, that window is automatically updated with a 'colon notation' of the new set, and so optimizing the room.

I hope this helps.

Jav

11 Dec 2008 Jos (10584)

First, could you give an exemplary situation in which this function is actually useful? Secondly, one should be aware of floating point issues here. Try this:
a = [.1 .2 .3 .4 .5 .6 .7 .8 .9 1] ;
str = vect2colon(a)
B = eval(str)
B==a % should be but they are not!

Updates
14 Nov 2008

Description was fixed.

29 Nov 2008

P1:Bug, two elements appeared without square brackets ([]).
R1:Fixed JLC, 11/29/2008

10 Dec 2008

Include options: 'DelimiterEnable' & 'Sort'

18 Mar 2011

 fixed bug where the increment is negative
 Checks numeric input
 Allows to keep repeated values
 mat2colon function was implemented (see submission)

Contact us