The colon is one of the most useful operators in MATLAB®.It can create vectors, subscript arrays, & specify for iterations.

Bạn đang xem: Vectashield® antifade mounting medium with dapi

x = j:k createsa unit-spaced vector x with elements where m= fix(k-j). If j và k areboth integers, then this is simply .

x = j:i:k createsa regularly-spaced vector x using i asthe increment between elements. The vector elements are roughly equalto where m = fix((k-j)/i).However, if i is not an integer, then floatingpoint arithmetic plays a role in determining whether colon includesthe endpoint k in the vector, since k mightnot be exactly equal lớn j+m*i.If you specify nonscalar arrays, then MATLAB interprets j:i:k as j(1):i(1):k(1).

x = colon(j,k) and x = colon(j,i,k) arealternate ways to execute the commands j:k và j:i:k,but are rarely used. These syntaxes enable operator overloading forclasses.

A(:,n), A(m,:), A(:),and A(j:k) are common indexing expressions fora matrix A that contain a colon. When you use acolon as a subscript in an indexing expression, such as A(:,n),it acts as shorthand to include all subscriptsin a particular array dimension. It is also common lớn create a vectorwith a colon for the purposes of indexing, such as A(j:k).Some indexing expressions combine both uses of the colon, as in A(:,j:k).

Common indexing expressions that contain a colon are:

A(:,n) is the nthcolumn of matrix A.

A(m,:) is the mthrow of matrix A.

A(:,:,p) is the pthpage of three-dimensional array A.

A(:) reshapes all elements of A intoa single column vector. This has no effect if A isalready a column vector.

A(:,:) reshapes all elements of A intoa two-dimensional matrix. This has no effect if A isalready a matrix or vector.

A(j:k) uses the vector j:k toindex into A và is therefore equivalent to thevector .

A(:,j:k) includes all subscriptsin the first dimension but uses the vector j:k toindex in the second dimension. This returns a matrix with columns .

## Examples

collapse all

### Create Unit-Spaced Vector

Open Live Script

Create a unit-spaced vector of numbers between 1 and 10. The colon operator uses a default increment of +1.

x = 1:10
x = 1×10 1 2 3 4 5 6 7 8 9 10

### Create Vector with Specified Increment

Open Live Script
Create vectors that increment or decrement by a specified value.

Create a vector whose elements increment by 0.1.

x = 0:0.1:1
x = 1×11 0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000 0.9000 1.0000
Create a vector whose elements decrement by -2.

y = 10:-2:0
y = 1×6 10 8 6 4 2 0

### Index Matrix Rows & Columns

Open Live Script
Examine several ways khổng lồ index a matrix using a colon :.

Create a 3-by-3 matrix. Index the first row.

A = magic(3)
A = 3×3 8 1 6 3 5 7 4 9 2
A(1,:)
ans = 1×3 8 1 6
Index the second & third column.

A(:,2:3)
ans = 3×2 1 6 5 7 9 2
Reshape the matrix into a column vector.

A(:)
ans = 9×1 8 3 4 1 5 9 6 7 2

### Specify for-loop Iterations

Open Live Script
In the context of a for-loop, the colon specifies the loop iterations.

Write a for-loop that squares a number for values of n between 1 & 4.

for n = 1:4 n^2end
ans = 1
ans = 4
ans = 9
ans = 16

## Input Arguments

collapse all

### j — Starting vector value scalar

Starting vector value, specified as a real numeric scalar. If j so that the đầu ra vector is not empty, then j isthe first element in the vector.

Example: x = 0:5

Example: x= 0:0.5:5

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | char | datetime | duration

### k — Ending vector value scalar

Ending vector value, specified as a real numeric scalar. K isthe last value in the vector only when the increment lines up to exactlyland on k. For example, the vector 0:5 includes5 as the last value, but 0:0.3:1 does not includethe value 1 as the last value since the increment does not line upwith the endpoint.

Example: x = 0:5

Example: x= 0:0.5:5

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | char | datetime | duration

### i — Increment between vector elements 1 (default) | scalar

Increment between vector elements, specified as a real numericscalar.

Example: x = 0:0.5:5

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | char | datetime | duration

## Output Arguments

collapse all

### x — Regularly-spaced vector row vector

Regularly-spaced vector, returned as a row vector. If j> k, then x = j:k is an empty matrix.More generally, the syntax x = j:i:k returns anempty matrix when:

i, j, or k isan empty input

i == 0

i > 0 and j > k

i and j

## Extended Capabilities

### Tall Arrays Calculate with arrays that have more rows than fit in memory.

This function supports tall arrays with some limitations. Formore information, see Index and View Tall Array Elements.

### C/C++ Code Generation Generate C và C++ code using MATLAB® Coder™.

Usage notes và limitations:

Complex inputs are not supported.

The đầu vào i cannot have a logicalvalue.

Vector inputs are not supported.

Inputs must be constants.

Uses single-precision arithmetic lớn produce single-precisionresults.

### GPU Arrays Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.

Usage notes & limitations:

To run this function on a GPU and obtain a gpuArray output, use any of the following syntaxes:
x = gpuArray.colon(j,k)x = gpuArray.colon(j,i,k)

Alternatively, you can also pass gpuArray objects directly to the colon operator:
x = gpuArray(j):gpuArray(k)x = gpuArray(j):gpuArray(i):gpuArray(k)

64-bit integers are not supported.

For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

### Distributed Arrays Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.

This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).

## Version History

Introduced before R2006a

for | linspace | logspace | reshape | varargin

×

## Open Example

You have a modified version of this example. Bởi you want to mở cửa this example with your edits?

No, overwrite the modified version Yes
×

## MATLAB Command

You clicked a links that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Website browsers vì not tư vấn MATLAB commands.

Close
×

Select a website Site

Choose a website site khổng lồ get translated content where available & see local events & offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

## How lớn Get Best Site Performance

Select the china site (in Chinese or English) for best site performance. Other chungcutuhiepplaza.com country sites are not optimized for visits from your location.

Xem thêm: Sai Phân - Phương Trình Vi Phân

Switzerland