Search code examples
matlabvectormatrix-indexing

applying arrayfun on n-dimensional matrixes


i need your help in solving the following problem:

how can i generalize the following for any n dimensional array:

reshape(arrayfun(@(x,y)sprintf('%d,%d',x,y),C{:},'un',0),size(M));

M is my matrix and C is my matrix of indexes of M.

thanks in advance.


Solution

  • The problem isn't the number of dimensions in the arguments to arrayfun per se, but the number of arguments themselves - which happens in your example to correspond to the number of dimensions each argument has. You therefore need to pass it a function that accepts varargin, which still works on an anonymous function:

    reshape(arrayfun(@(varargin)sprintf(strjoin(repmat('%d',size(varargin)),','),varargin{:}),C{:},'un',0),size(M));