2 views (last 30 days)

A={[45;101],[2;7],[5;8],0,0};

A(cell2mat(A)==0)={[]}; % I want to find zero and put in empty

A should be

A={[45;101],[2;7],[5;8],[],[]};

Guillaume
on 7 Jan 2020

Edited: Guillaume
on 7 Jan 2020

A(cellfun(@(x) isequal(x, 0), A)) = {[]}; %replace any cell whose content is the scalar 0 by empty

is one way.

edit: fixed code.

Stephen Cobeldick
on 7 Jan 2020

Actually the original idea is fine, it just needs a scalar cell array on the RHS:

>> A = {[45;101],[2;7],[5;8],0,0}

A =

[2x1 double] [2x1 double] [2x1 double] [0] [0]

>> A(cellfun(@(x)isequal(x,0), A)) = {[]} % SCALAR CELL ARRAY

A =

[2x1 double] [2x1 double] [2x1 double] [] []

When allocating to the LHS the RHS can always be a scalar, and this applies to cell array just like any other array type. Instead of making this more complex, all that is required is the scalar cell array {[]} rather than the empty cell array {}.

Guillaume
on 7 Jan 2020

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.