Differences between revisions 2 and 4 (spanning 2 versions)
Revision 2 as of 2025-10-25 03:41:11
Size: 1785
Comment: Shorthand
Revision 4 as of 2025-10-25 04:10:23
Size: 2593
Comment: Assignment
Deletions are marked like this. Additions are marked like this.
Line 6: Line 6:

----



== Description ==

All data types in MATLAB are arrays.

Elements of an array are accessed by subscripting; note that arrays index from 1.

{{{
first = A(1)
last = A(end)
middle = A(2:end-1)
}}}

Note that `2:end-1` expands to the row vector `[2 3 ... end-1 end]`. In other words, any row vector can be used as a subscript, and the elements of a subscripting row vector do not need to be sequential.

Assignment works as expected.

{{{
A(2) = 1
A(1) = A(2)
}}}

Matrices work in the same way by are indexed by row then column.

{{{
a = A(1,2)
}}}

If only one index is provided when subscripting a matrix, it is treated as a flattened vector by stacking all columns.

Rows and columns can be accessed from a matrix like:

{{{
row = A(1,:)
column = A(:,1)
}}}

MATLAB Data Types

MATLAB exposes numeric, character array, and string data types.


Description

All data types in MATLAB are arrays.

Elements of an array are accessed by subscripting; note that arrays index from 1.

first = A(1)
last = A(end)
middle = A(2:end-1)

Note that 2:end-1 expands to the row vector [2 3 ... end-1 end]. In other words, any row vector can be used as a subscript, and the elements of a subscripting row vector do not need to be sequential.

Assignment works as expected.

A(2) = 1
A(1) = A(2)

Matrices work in the same way by are indexed by row then column.

a = A(1,2)

If only one index is provided when subscripting a matrix, it is treated as a flattened vector by stacking all columns.

Rows and columns can be accessed from a matrix like:

row = A(1,:)
column = A(:,1)


Numeric

All numeric variables are arrays. Colloquially, an array with a single element is called a scalar.

By default, all numeric values are stored as double-precision floating point. Other storage sizes are available however.

x = 3;
y = [3 5];

A shorthand for an array of sequential numbers (with an optional step parameter) is:

a = 5:8;
b = 20:2:24;


Storage Size

To declare a numeric array with a specific storage size, try:

y = uint64(10);

These declaration functions include:

  • double()

  • single()

  • int8()

  • int16()

  • int32()

  • int64()

  • uint8()

  • uint16()

  • uint32()

  • uint64()


Type Casting

The cast function is used to change the storage size of a numeric array. Try:

b = cast(a,"uint8");

The second argument must be one of:

  • "single"

  • "double"

  • "int8"

  • "int16"

  • "int32"

  • "int64"

  • "uint8"

  • "uint16"

  • "uint32"

  • "uint64"

  • "logical"

  • "char"


Character Array

Character arrays store a sequence of characters. They are declared like:

c = 'Hello World';


String

All string variables are arrays. Colloquially, a string array with a single element is called a string scalar.

str = "Greetings friend"
str = ["Greetings friend" "Hello, world"]

To convert a numeric array into a string array, try:

>> str = string([1 24 356]);

str = 1x3 string

   "1"          "24"          "356"


CategoryRicottone

MATLAB/DataTypes (last edited 2025-12-17 19:52:15 by DominicRicottone)