Vector to colon notation
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.
Zitieren als
Javier Lopez-Calderon (2024). Vector to colon notation (https://www.mathworks.com/matlabcentral/fileexchange/22076-vector-to-colon-notation), MATLAB Central File Exchange. Abgerufen .
Kompatibilität der MATLAB-Version
Plattform-Kompatibilität
Windows macOS LinuxKategorien
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Live Editor erkunden
Erstellen Sie Skripte mit Code, Ausgabe und formatiertem Text in einem einzigen ausführbaren Dokument.
Version | Veröffentlicht | Versionshinweise | |
---|---|---|---|
1.6.0.0 | fixed bug where the increment is negative
|
||
1.3.0.0 | Include options: 'DelimiterEnable' & 'Sort' |
||
1.2.0.0 | P1:Bug, two elements appeared without square brackets ([]).
|
||
1.1.0.0 | Description was fixed. |
||
1.0.0.0 |