Also, if you tried anything bigger than 9 in my example, it would also give you an error. For example if you tried to specify the index of 5 in your example, it would give you an error. NB: You have to make sure that you are not providing indexes that would make the accessing out of bounds. In order to be complete, let's do this for a vector: V = % A 6 x 1 vector G = A(F) % G should give Īs such, the output when you access elements this way is whatever the size of the vector or matrix that you specify as the argument.
#Matlab b code#
Here is some MATLAB code to strengthen your understanding for this kind of indexing: A = % 3 x 3 matrixĮ = A(D) % E should give Let's define a matrix A such that: A = [5 1 3 Here are some examples to further your understanding. So A(5) would be 2, A(6) would be 4 and so on. Once you hit the 5th index, it skips over to the next column. You would start with the first column, and increasing indices will traverse down the first column. For example, supposing we had this matrix: A = [1 2Ī(1) would be 1, A(2) would be 3, A(3) would be 5 and so on. Not only can you do this for a vector, but you can do this for a matrix as well.īear in mind that when you're doing this for a matrix, you access the elements in column major format. The output is basically the same size as b, and you are creating a matrix where there are 3 rows, and each element accesses the first, second and third element. By doing a(b) to access the vector, what you are essentially doing is a lookup. Let's also say we had b as a matrix such that it was b =.
![matlab b matlab b](https://image2.slideserve.com/5316139/b-matlab-introduction-l.jpg)
A(1), A(2) and so on).įor example, let's say we had a vector a =. Accessing values in MATLAB is not just limited to single indices (i.e. When you have a vector or a matrix, you can provide another vector or matrix in order to access specific values.
![matlab b matlab b](https://i.stack.imgur.com/lFT5H.jpg)
That's a very standard MATLAB operation that you're doing. However, I would like to duplicate my answer here as I think it is informative. This is a possible duplicate to this post where I gave an answer: Got confused with a vector indexed by a matrix, in Matlab