5

I am wondering if we can pass cell indexing as an input argument to a function, such that a cell inside the function can parse it.

For example, given a cell like x = num2cell(reshape(1:24, [2,3,4])), we can do indexing

>> x(:,1,2)

ans =

  2×1 cell array

    {[7]}
    {[8]}

However, if I want to pass (:,1,2) as an input argument to a custom function (see the attempt below, which is not working for sure), what can we do?

f({:,1,2}) % not working

function y = f(varargin)
x = num2cell(reshape(1:24, [2,3,4]));
y = x(varargin{:});
end
1
  • 4
    There are two issues here. 1) varargin here itself will be a cell. So if you go inside your function f at runtime, you will see that varargin is a cell that contains 1 element - another cell (this is what contains the inputs). When you then call x(varargin{:}), you are trying to use the inner cell as in index - hence the error. If you change this to f(2,1,2), it will now work. 2) the use of :. The colon operator cannot be directly passed if the context is not known. Hence, f(:,1,2) will not work. You can use the colon character ':' instead - f(':',1,2) will work. Commented Jul 23 at 8:48

2 Answers 2

5
  1. Because you are using a cell array to input the indices, varargin is a cell array with your cell nested as the first element. You don't need varargin and can simply let f expect a single input which is your indexing cell

  2. You can't pass : around like this, but : and the char ':' will be treated the same when indexing so you can do

f({':',1,2}) % Note the colon operator is a character

function y = f(idx)
% Take an indexing array 'idx' and use it to index a subset
% of the generated array 'x'
x = num2cell(reshape(1:24, [2,3,4]));
y = x(idx{:});
end

Alternatively, if you wanted to use varargin to achieve the same functionality, you could do

f(':',1,2) % Note the colon operator is a character

function y = f(varargin)
% Take an indexing array 'idx' and use it to index a subset
% of the generated array 'x'
x = num2cell(reshape(1:24, [2,3,4]));
y = x(varargin{:});
end

The 2nd option means you don't need to put the indices into a cell array, but would probably be less clear if you had other inputs to f.


Here is a MathWorks blog post about passing around indices in cell arrays, including a reference to using the colon operator in quotes:

https://blogs.mathworks.com/loren/2006/11/10/all-about-the-colon-operator/

Sign up to request clarification or add additional context in comments.

1 Comment

This is excellent! Thanks for the elegant solution!
0

I found one possible workaround, but will keep the question open for more options

function y = f(indexingStruct)
x = num2cell(reshape(1:24, [2,3,4]));
y = subsref(x, indexingStruct);
end

indexingPattern = substruct('()', {':', 1, 2});
f(indexingPattern)

which gives

ans =

  2×1 cell array

    {[7]}
    {[8]}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.