create an array in matlab

To create an array in MATLAB, you can use the following syntax:

main.m
myArray = [element1, element2, element3, ...];
47 chars
2 lines

For example, to create an array of integers from 1 to 5, you can use:

main.m
myArray = [1, 2, 3, 4, 5];
27 chars
2 lines

You can also create arrays using the linspace and logspace functions.

main.m
x = linspace(0, 1, 100);     % Creates an array of 100 linearly spaced values between 0 and 1
y = logspace(0, 1, 100);     % Creates an array of 100 logarithmically spaced values between 10^0 and 10^1
201 chars
3 lines

You can access the values of an array using indexing. For example:

main.m
myArray(1)    % Returns the first element (1)
myArray(3)    % Returns the third element (3)
92 chars
3 lines

Arrays in MATLAB can also be multidimensional. For example, to create a 2D array, you can use:

main.m
my2DArray = [1, 2, 3; 4, 5, 6; 7, 8, 9];
41 chars
2 lines

This creates a 3-by-3 matrix containing the numbers 1 through 9. You can access elements of a multidimensional array using multiple indices. For example:

main.m
my2DArray(1, 2)    % Returns the element in the first row and second column (2)
my2DArray(3, 3)    % Returns the element in the third row and third column (9)
159 chars
3 lines

gistlibby LogSnag