Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use the <a href="http://www.mathworks.com/help/techdoc/ref/sub2ind.html" rel="nofollow"><code>sub2ind</code></a> function to get the linear index from the subscript.</p> <p><strong>Example:</strong></p> <pre><code>A=magic(4) A = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 selectElement={2,3}; %# get the element at position 2,3 in A. indx=sub2ind(size(A),selectElement{:}); A(indx) ans = 10 </code></pre> <p>In the above example, I've stored the subscripts (can be any number of dimensions) as a <code>cell</code>. If you have it stored as a vector, simply use <a href="http://www.mathworks.com/help/techdoc/ref/num2cell.html" rel="nofollow"><code>num2cell()</code></a> to convert it to a cell.</p> <p>You can now easily assign a value to this as <code>A(indx)=value;</code>. I've used different variables than yours to keep the answer general, but the idea is the same and you just need to replace the variable names.</p> <p>You also mentioned in your post that you're looping from <code>(1,1,1)</code> till some value, <code>(1000,15,3)</code> and assigning a value to each of these points. If you're looping along the columns, you can replace this entire operation with a vectorized solution. </p> <p>Let <code>finalElement={1000,15,3}</code> be the final step of the loop. As before, find the linear index as </p> <pre><code>index=sub2ind(size(A),finalElement{:}); </code></pre> <p>Now if you have the values you assign in the loop stored as a single vector, <code>values</code>, you can simply assign it in a single step as </p> <pre><code>A(1:index)=values; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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. 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