In MATLAB, the most basic data type is the numeric array. It can be a scalar, a 1-D vector, a 2-D matrix, or an N-D multidimensional array.

```
% a 1-by-1 scalar value
x = 1;
```

To create a row vector, enter the elements inside brackets, separated by spaces or commas:

```
% a 1-by-4 row vector
v = [1, 2, 3, 4];
v = [1 2 3 4];
```

To create a column vector, separate the elements with semicolons:

```
% a 4-by-1 column vector
v = [1; 2; 3; 4];
```

To create a matrix, we enter the rows as before separated by semicolons:

```
% a 2 row-by-4 column matrix
M = [1 2 3 4; 5 6 7 8];
% a 4 row-by-2 column matrix
M = [1 2; ...
4 5; ...
6 7; ...
8 9];
```

Notice you cannot create a matrix with unequal row / column size. All rows must be the same length, and all columns must be the same length:

```
% an unequal row / column matrix
M = [1 2 3 ; 4 5 6 7]; % This is not valid and will return an error
% another unequal row / column matrix
M = [1 2 3; ...
4 5; ...
6 7 8; ...
9 10]; % This is not valid and will return an error
```

To transpose a vector or a matrix, we use the `.'`

-operator, or the `'`

operator to take its Hermitian conjugate, which is the complex conjugate of its transpose. For real matrices, these two are the same:

```
% create a row vector and transpose it into a column vector
v = [1 2 3 4].'; % v is equal to [1; 2; 3; 4];
% create a 2-by-4 matrix and transpose it to get a 4-by-2 matrix
M = [1 2 3 4; 5 6 7 8].'; % M is equal to [1 5; 2 6; 3 7; 4 8]
% transpose a vector or matrix stored as a variable
A = [1 2; 3 4];
B = A.'; % B is equal to [1 3; 2 4]
```

For arrays of more than two-dimensions, there is no direct language syntax to enter them literally. Instead we must use functions to construct them (such as `ones`

, `zeros`

, `rand`

) or by manipulating other arrays (using functions such as `cat`

, `reshape`

, `permute`

). Some examples:

```
% a 5-by-2-by-4-by-3 array (4-dimensions)
arr = ones(5, 2, 4, 3);
% a 2-by-3-by-2 array (3-dimensions)
arr = cat(3, [1 2 3; 4 5 6], [7 8 9; 0 1 2]);
% a 5-by-4-by-3-by-2 (4-dimensions)
arr = reshape(1:120, [5 4 3 2]);
```

This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0

This website is not affiliated with Stack Overflow