![]() For example, A(A > 12) extracts all the elements of A that are greater than 12. The output is always in the form of a column vector. MATLAB extracts the matrix elements corresponding to the nonzero values of the logical array. ![]() In logical indexing, you use a single, logical array for the matrix subscript. This form of indexed assignment is called scalar expansion.Īnother indexing variation, logical indexing, has proven to be both useful and expressive. You can always, however, use a scalar on the right side: v() = 30 % Replace second and third elements by 30 Usually the number of elements on the right must be the same as the number of elements referred to by the indexing expression on the left. ![]() V(end:-1:1) % Reverse the order of elementsīy using an indexing expression on the left side of the equal sign, you can replace certain elements of the vector: v() = % Replace some elements of v You can even do arithmetic using end: v(2:end-1) % Extract the second through the next-to-last elementsĬombine the colon operator and end to achieve a variety of effects, such as extracting every k-th element or flipping the entire vector: v(1:2:end) % Extract all the odd elements The end operator can be used in a range: v(5:end) % Extract the fifth through the last elements The special end operator is an easy shorthand way to refer to the last element of v: v(end) % Extract the last element Swap the two halves of v to make a new vector: v2 = v() % Extract and swap the halves of v The colon notation in MATLAB provides an easy way to extract a range of elements from v: v(3:7) % Extract the third through the seventh elements Or the subscript can itself be another vector: v() % Extract the first, fifth, and sixth elements The subscript can be a single value: v(3) % Extract the third element There are also applications in numerical methods, for example in assigning values to the elements of a matrix or vector.Let's start with the simple case of a vector and a single subscript. Second is when you want to analyze one part of the solution. The most common place to use indexing is probably when a function returns an array with the independent variable in column 1 and solution in column 2, and you want to plot the solution. think about the indexing like this: (row, column, page) M = randn(3,3,3) % a 3x3x3 array The 3d array is like book of 2D matrices. Using indexing to assign values to rows and columns b = zeros(size(a)) The syntax is to use a colon a(1,:) % first rowĪ(:) % all the elements of the a array as a column vector To get a row, we specify the row number we want, and we need a syntax to specify every column index in that row. In a 2D array, you index with (row,column) a = ] We can use the mask on other vectors too, to get the y-values where x > 2, for example, and then to integrate that subsection of data (or some other analysis). X(ind) % use indexing to get the part of x where x > 2 We can create a mask of boolean (0 or 1) values that specify whether x > 2 or not, and then use the mask ind = x > 2 We could do that by inspection, but there is a better way. Suppose we want the part of the vector where x > 2. The syntax a:n:b gets the elements starting at index a, skipping n elements up to the index b x(1:3:end) % every third element It is possible to get a single element, a range of elements, or all the elements. We use the parentheses operator to index.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |