Get notified about new tutorials
RECEIVE NEW TUTORIALS

First 15 Minutes Free

290 sessions given

since Jul 09, 2014

since Jul 09, 2014

Likelihood of Reply:
75%

Response Time:
within an hour

Ray Phan

Feb 02, 2015

<p>That's a very standard MATLAB operation that you're doing. When you have a vector or a matrix, you can provide another vector or matrix in order to access specific values. Accessing values in MATLAB is not just limited to single indices (i.e. <code>A(1), A(2)</code> and so on).</p>
<p>For example, what you have there is a vector of <code>a = [1 2 3 4]</code>. When you try to use <code>b</code> to access the vector, what you are essentially doing is a lookup. The output is basically the same size as <code>b</code>, and what you are doing is creating a matrix where there are 3 rows, and each element accesses the first, second and third element. Not only can you do this for a vector, but you can do this for a matrix as well. </p>
<p>Bear in mind that when you're doing this for a matrix, you access the elements in <strong>column major format</strong>. For example, supposing we had this matrix:</p>
<pre><code> A = [1 2
3 4
5 6
7 8]
</code></pre>
<p><code>A(1)</code> would be 1, <code>A(2)</code> would be 3, <code>A(3)</code> would be 5 and so on. You would start with the first column, and increasing indices will traverse down the first column. Once you hit the 5th index, it skips over to the next column. So <code>A(5)</code> would be 2, <code>A(6)</code> would be 4 and so on.</p>
<p>Here are some examples to further your understanding. Let's define a matrix <code>A</code> such that:</p>
<pre><code> A = [5 1 3
7 8 0
4 6 2]
</code></pre>
<p>Here is some MATLAB code to strengthen your understanding for this kind of indexing:</p>
<pre><code> A = [5 1 3; 7 8 0; 4 6 2]; % 3 x 3 matrix
B = [1 2 3 4];
C = A(B); % C should give [5 7 4 1]
D = [5 6 7; 1 2 3; 4 5 6];
E = A(D); % E should give [8 6 3; 5 7 4; 1 8 6]
F = [9 8; 7 6; 1 2];
G = A(F); % G should give [2 0; 3 6; 5 7]
</code></pre>
<p>As such, the output when you access elements this way is whatever the size of the vector or matrix that you specify as the argument.</p>
<p>In order to be complete, let's do this for a vector:</p>
<pre><code> V = [-1 9 7 3 0 5]; % A 6 x 1 vector
B = [1 2 3 4];
C = V(B); % C should give [-1 9 7 3]
D = [1 3 5 2];
E = V(D); % E should give [-1 7 0 9]
F = [1 2; 4 5; 6 3];
G = V(F); % G should give [-1 9; 3 0; 5 7]
</code></pre>
<p><strong>NB:</strong> You have to make sure that you are not providing indexes that would make the accessing out of bounds. For example if you tried to specify the index of 5 in your example, it would give you an error. Also, if you tried anything bigger than 9 in my example, it would also give you an error. There are 9 elements in that 3 x 3 matrix, so specifying a column major index of anything bigger than 9 will give you an out of bounds error.</p>
<p>This tip was originally posted on <a href="http://stackoverflow.com/questions/22997632/Got%20confused%20with%20a%20vector%20indexed%20by%20a%20matrix,%20in%20Matlab/22997867">Stack Overflow</a>.</p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.