working with arrays in matlab video -凯发k8网页登录
create and manipulate matlab® arrays, including accessing elements using indexing.
matlab stores all types of data in arrays. this includes not only numeric data, but data of other types such as strings or even complex objects. so working with arrays is fundamental to working with matlab. with the matlab language, you can create arrays, access and assign values to array elements using a number of indexing methods, and perform many other operations to manipulate the array's contents.
let's first look at creating arrays. you can create an array by specifying specific values using square brackets and commas or spaces to separate columns in a row such as a equals 1, 2, 3, 4 and semicolons to separate rows. you can create equally spaced one dimensional arrays with a column operator such as a equals 1 to 10, a equals 1 to 10 in steps of 2, or a equals 10 to 1 in steps of negative 2.
the linspace space function is similar to the colon operator, letting you specify a start and end value but gives control over the number of points such as 7. you can change the rows to columns with the transpose operator. you can also call a number of functions that generate elementary matrices with different contents such as ones, zeros, or random numbers. it can be more convenient to inspect the contents of an array by opening it into the variable editor.
let's now look at how you can access and change the values of array elements with different forms of indexing. you can specify elements of an array by simple row and column indexing. here is the element of a in the first row second column.
you can specify a range of rows and columns to access sections of an array such as row 1, columns 1 through 2. the elements do not have to be contiguous, such as row 1, columns 1 and 3. you can specify all rows or columns by using the colon operator, in this case specifying all columns. you can also use the end keyword such as row 1, columns 2 to the end or 2 the end minus 1.
you can assign values to specific elements by specifying indexing on the left hand side of the equation such as rows 1, columns 2 to the end minus 1 equals 10 10. you can delete one or more rows of an array such as rows 1 to 2, all the columns, by assigning them to the empty matrix denoted by square brackets. a is now two rows shorter.
sometimes it is convenient to treat two dimensional arrays such as these as a one dimensional array as though all the columns were stacked together into a single column and specify single index. this is called linear indexing. for example, the element at row 1 column 2 can be accessed through one linear index, 5.
this is possible because matlab arrays are stored column wise in memory. in other words, each column in the array is stored one after another. so the element at row 1 column 2 is, in fact, the fifth element stored. the colon operator used on its own specifies all elements when using linear indexing, and it returns a single column vector with the entire array contents.
you can also access elements with what is known as logical indexing where you specify an indexing array of equal size filled with true or false values, like a mask. this is useful for operating on elements whose values match some criterion such as a is less than 0.5, which creates an array of logical values the same size as a with true values displayed here as 1 whenever a is less than 0.5. note logical values like true and false are displayed as 1 and 0s respectively.
we can use logical arrays such as this to perform logical indexing such as set the elements of a where a is less than 0.5 to negative 1. to find the indices of array elements that match your criteria, use the find function, which finds non-zero values together with a logical expression. this gives the linear indices of the elements that meet the condition a is less than 0.5. you can get the rows and column indices instead. you can see information about all indexing techniques in the documentation.
and finally, let's look at how you can extract some useful information about an array and perform some basic operations. you can get basic information on an array such as determine if it's empty, get the length-- usually used for 1d arrays-- size of all the dimensions, or the total number of elements. as we saw before, creating arrays with square brackets lets us concatenate a number of arrays together horizontally or vertically. other useful array manipulation functions include flip left-right and flip up-down, repmat to replicate matrices, reshape, and sort.
although the examples shown here use one and two dimensional arrays, most of these techniques can be applied to multi-dimensional arrays, as well. see the documentation for more information. this concludes the demonstration. try these features in matlab now or watch one of the other videos.
featured product
matlab
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 mathworks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- (español)
- (english)
- (english)
欧洲
- (english)
- (english)
- (deutsch)
- (español)
- (english)
- (français)
- (english)
- (italiano)
- (english)
- (english)
- (english)
- (deutsch)
- (english)
- (english)
- switzerland
- (english)
亚太
- (english)
- (english)
- (english)
- 中国
- (日本語)
- (한국어)