5.3 Index matricesĪs well as an index vector in any subscript position, a matrix may be used with a single index matrix in order either to assign a vector of quantities to an irregular collection of elements in the array, or to extract an irregular collection as a vector.Ī matrix example makes the process clear. This is not the case, however, if the single index is not a vector but itself an array, as we next discuss. a stands for the entire array, which is the same as omitting the subscripts entirely and using a alone.įor any array, say Z, the dimension vector may be referenced explicitly as dim(Z) (on either side of an assignment).Īlso, if an array name is given with just one subscript or index vector, then the corresponding values of the data vector only are used in this case the dimension vector is ignored. More generally, subsections of an array may be specified by giving a sequence of index vectors in place of subscripts however if any index position is given an empty index vector, then the full range of that subscript is taken.Ĭontinuing the previous example, a is a 4 * 2 array with dimension vector c(4,2) and data vector containing the valuesĬ(a, a, a, a, a, a, a, a) Individual elements of an array may be referenced by giving the name of the array followed by the subscripts in square brackets, separated by commas. , a, a.Īrrays can be one-dimensional: such arrays are usually treated in the same way as vectors (including when printing), but the exceptions can cause confusion. The values in the data vector give the values in the array in the same order as they would occur in FORTRAN, that is “column major order,” with the first subscript moving fastest and the last subscript slowest.įor example if the dimension vector for an array, say a, is c(3,4,2) then there are 3 * 4 * 2 = 24 entries in a and the data vector holds them in the order a, a. Other functions such as matrix() and array() are available for simpler and more natural looking assignments, as we shall see in The array() function. You can use the sub2ind function to get the linear index from the subscript.Gives it the dim attribute that allows it to be treated as a 3 by 5 by 100 array. Is there a simple way to use a vector as the points in an index? I can't just use dummy variables because sometimes the matrix will need 3 indexes, other times 4, other times 2, etc, so a vector of variable length is really what I need here. What I'd like to be able to do is feed current_point as an index to the matrix like so: output_matrix(current_point) = valīut apparently something like output_matrix() = val will just set outputmatrix(1:3) = 30. So, if for example I wanted a 3-dimensional array of size 1000-by-15-by-3, max_points =, and current_point iterates from to ( -> -> -> ->.). I need to be able to access a specific point in the Matrix (to write to it or read it, for example), but I don't know ahead of time how many indexes to specify.Ĭurrently I have a current_point vector which I iterate through to specify each index, and a max_points vector which specifies the size of the array. I'm writing a MATLAB function to read out data into an n-dimensional array (variable dimension size).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |