Note that there are some explanatory texts on larger screens.

plurals
  1. PORetrieving element index in spfun, cellfun, arrayfun, etc. in MATLAB
    text
    copied!<p>Is there any way to retrieve the <strong>index</strong> of the element on which a function called by <code>cellfun</code>, <code>arrayfun</code> or <code>spfun</code> acts? (i.e. retrieve the index of the element within the scope of the function). </p> <p>For the sake of simplicity, imagine I have the following toy example:</p> <pre><code>S = spdiags([1:4]',0,4,4) f = spfun(@(x) 2*x,S) </code></pre> <p>which builds a 4x4 sparse diagonal matrix and then multiplies each element by <code>2</code>.</p> <p>And say that now, instead of multiplying each element by the constant number <code>2</code>, I would like to multiply it by <strong>the index the element has in the original matrix</strong>, i.e. assuming that <code>linear_index</code> holds the index for each element, it would be something like this: </p> <pre><code>S = spdiags([1:4]',0,4,4) f = spfun(@(x) linear_index*x,S) </code></pre> <p>However, note the code above does not work (<code>linear_index</code> is undeclared).</p> <p>This question is partially motivated by the fact that <code>blocproc</code> gives you access to <code>block_struct.location</code>, which one could argue references the location (~index) of <em>the current element</em> within the <em>full object</em> (an image in this case):</p> <blockquote> <p><code>block_struct.location:</code> A two-element vector, [row col], that specifies the position of the first pixel (minimum-row, minimum-column) of the block data in the input image.</p> </blockquote>
 

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