Differences between revisions 1 and 2
Revision 1 as of 2025-10-22 17:59:25
Size: 1065
Comment: Initial commit
Revision 2 as of 2025-10-22 19:04:25
Size: 1511
Comment: Matrices
Deletions are marked like this. Additions are marked like this.
Line 32: Line 32:
== Arrays and Matrices ==

All data are arrays. To declare a variable with more than 1 element, try:

{{{
r=[3 5]
}}}

This is a '''row vector'''. To instead declare a '''column vector''', try:

{{{
c=[3; 5]
}}}

To declare a '''matrix''', mix the two syntaxes:

{{{
M=[1 2; 3 4]
}}}

To concatenate arrays horizontally, try:

{{{
A=[c M]
}}}

To concatenate arrays vertically, try:

{{{
B=[r; M]
}}}

----


MATLAB Variables

Variables contain statically-typed data.


Declaration

Variables are declared using the equals sign (=). To suppress printing of the value assigned, complete the declaration with a semicolon (;).

>> m=3*5

m =

    15
>> m=3*5;

Variable names must start with a letter and must contain only letters, numbers, and underscores (_). Variable names are case-sensitive.

If an interactive command tries to declare a variable with an invalid name, the prompt is re-filled where the name is replaced with a suggested 'fix'.


Arrays and Matrices

All data are arrays. To declare a variable with more than 1 element, try:

r=[3 5]

This is a row vector. To instead declare a column vector, try:

c=[3; 5]

To declare a matrix, mix the two syntaxes:

M=[1 2; 3 4]

To concatenate arrays horizontally, try:

A=[c M]

To concatenate arrays vertically, try:

B=[r; M]


Workspace

Variables exist in a workspace. To clear the workspace, try clear.

To save all variables in the workspace to a file named like foo.mat, try save foo. To save a subset of variables instead, try save foo VARLIST.

To load a workspace from a file, try load foo. To load a subset of variables contained in that file, try load foo VARLIST.


CategoryRicottone

MATLAB/Variables (last edited 2026-01-19 15:48:57 by DominicRicottone)