Search code examples
arraysmatlabsubstitutioncell-array

Combine cellfun and subs in Matlab


I have some values stored in a matrix, e.g.

Matrix = [1,4,6]

and a cell array, such as:

CellArray{1} = [0,0,1,0]
...
CellArray{4} = [0,0,0,0,0,0,0,1]
...
CellArray{6} = [0,0,1,1,1,0]

For each element of the Matrix in CellArray, i.e. CellArray{Matrix(1:end)}, I want to substitute the ones with zeros. So far, I've thought of:

[Output] = cellfun(@(x) subs(x,1,0),{CellArray{Matrix}},'UniformOutput',false)

though, the ouput is not as I wanted...


Solution

  • Since the example suggests that CellArray only contains vectors with 0s and 1s (and the question does not specify the opposite), may I suggest

    Output = cellfun(@(x)zeros(1, numel(x)), CellArray(Matrix), 'uniformoutput', 0)
    

    which really just replaces the entry with a zero vector of appropriate length.