Is there any way to retrieve the index of the element on which a function called by cellfun
, arrayfun
or spfun
acts? (i.e. retrieve the index of the element within the scope of the function).
For the sake of simplicity, imagine I have the following toy example:
S = spdiags([1:4]',0,4,4)
f = spfun(@(x) 2*x,S)
which builds a 4x4 sparse diagonal matrix and then multiplies each element by 2
.
And say that now, instead of multiplying each element by the constant number 2
, I would like to multiply it by the index the element has in the original matrix, i.e. assuming that linear_index
holds the index for each element, it would be something like this:
S = spdiags([1:4]',0,4,4)
f = spfun(@(x) linear_index*x,S)
However, note the code above does not work (linear_index
is undeclared).
This question is partially motivated by the fact that blocproc
gives you access to block_struct.location
, which one could argue references the location (~index) of the current element within the full object (an image in this case):
block_struct.location:
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.