Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The short answer is this:</p> <pre><code> A(sub2ind(size(A),X,Y)) = V </code></pre> <p>Try it out, it works like a charm.</p> <p>The explanation, if you're curious and want it, is simple to follow:</p> <p>Every matrix in Matlab is stored as a 1d array, a normal vector.</p> <p>A 2d array is actually stored in memory as a sequence of the first column, followed by the second column, and so on. Because of that, you can index any Matlab array, independent of its dimension, by a linear subscript.</p> <p>For example:</p> <pre><code> A = [1 4 7; 2 4 8; 3 6 9 ]; </code></pre> <p>Is actually stored as: </p> <pre><code>[1 2 3 4 5 6 7 8 9] </code></pre> <p>So to access item all you have to do is: (j-1)*num_of_columns + i</p> <p>Because of that, A(2,3) returns the same as A(8), that is, in this case: 8;</p> <p>The sub2ind function transforms a list of "subscripts" to linear indexes, given the size of the matrix. Exactly what you want to do.</p> <p>And with the indexes at hand, it is easy to make the assignment.</p> <p>Docs: <a href="http://www.mathworks.com/help/techdoc/ref/sub2ind.html" rel="nofollow">sub2ind</a></p> <p><a href="http://www.mathworks.com/help/techdoc/math/f1-85462.html" rel="nofollow">Indexing in Matlab</a></p> <p>Hope I understood you correctly.</p> <p>I took the time to answer in a long fashion because this is a very important part of Matlab, and a LOT of people overcomplicate the code using the "vector-operating" functions to do things that could be done with normal indexing.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload