Search code examples
matlabindexingcelloctavematrix-indexing

Mablab/Octave - use cellfun to index one matrix with another


I have a cell containing a random number of matrices, say a = {[300*20],....,[300*20]};. I have another cell of the same format, call it b, that contains the logicals of the position of the nan terms in a.

I want to use cellfun to loop through the cell and basically let the nan terms equal to 0 i.e. a(b)=0.

Thanks, j


Solution

  • You could define a function that replaces any NaN with zero.

    function a = nan2zero(a)
      a(isnan(a)) = 0;
    

    Then you can use cellfun to apply this function to your cell array.

    a0 = cellfun(@nan2zero, a, 'UniformOutput', 0)
    

    That way, you don't even need any matrices b.