Home > other >  How to make specific elements of a cell equal to zero?
How to make specific elements of a cell equal to zero?

Time:09-16

I have a cell 41 * 41 dimension, and I would like to make the values over 0.5 equal to zeros in Matlab, and the rest of the values keep them without any changes. Is it possible to work directly on the cell, or do we need to convert it to a matrix first?

Any assistance, please?

My try

AAA = cell2mat( BBB );
for i = 1: length(AAA)
    for j = 1 : i
        if AAA(i,j) > 0.5
            AAA(i,i) = 0;
        else
            cc = AAA(i,j);
        end
    end
end

CodePudding user response:

Since logical indexing is not supported for operands of type 'cell', you have to convert to matrix first. Use logical indexing on the matrix and convert back to cell. Assuming BBB is like BBB = num2cell(rand(41)), then

AAA = cell2mat(BBB);
AAA(AAA > 0.5) = 0;
BBB = num2cell(AAA)

CodePudding user response:

You could use boolean indexing:

AAA[AAA > 0.5] = 0;
  • Related