Remove zeros from cell

20 views (last 30 days)
Adam
Adam on 27 Oct 2017
Answered: Star Strider on 27 Oct 2017
Dear all, I have the following problem:
mydata{n}(m,l)
let say n=1:1:3; m=1:1:4 and l=1:1:100
I have many zeros in cell. How can I remove them:
the result for mydata{1}(:,:) looks like :
0.223811939491137 0.751267059305653 0.255095115459269 0.505957051665142
0.699076722656686 0.890903252535798 0.959291425205444 0.547215529963803
0.138624442828679 0.149294005559057 0.257508254123736 0.840717255983663
0.254282178971531 0.814284826068816 0.243524968724989 0.929263623187228
0.349983765984809 0.196595250431208 0.251083857976031 0.616044676146639
0.473288848902729 0.351659507062997 0.830828627896291 0
0.549723608291140 0.917193663829810 0.285839018820374 0
0.753729094278495 0.380445846975357 0 0
0.053950118666607 0 0 0
0.129906208473730 0 0 0
Thanks in Advance ...
  1 Comment
Reza Bonyadi
Reza Bonyadi on 27 Oct 2017
mydata{i} is a two dimensional matrix. What would be the result if you remove zeros from that? What would you replace them by?

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 27 Oct 2017
This replaces the zeros with empty square brackets:
mydata = { 0.223811939491137 0.751267059305653 0.255095115459269 0.505957051665142
0.699076722656686 0.890903252535798 0.959291425205444 0.547215529963803
0.138624442828679 0.149294005559057 0.257508254123736 0.840717255983663
0.254282178971531 0.814284826068816 0.243524968724989 0.929263623187228
0.349983765984809 0.196595250431208 0.251083857976031 0.616044676146639
0.473288848902729 0.351659507062997 0.830828627896291 0
0.549723608291140 0.917193663829810 0.285839018820374 0
0.753729094278495 0.380445846975357 0 0
0.053950118666607 0 0 0
0.129906208473730 0 0 0};
zero_idx = bsxfun(@eq, [mydata{:}], 0);
mydata(zero_idx) = {[]};

More Answers (1)

KSSV
KSSV on 27 Oct 2017
On removing zeros...your matrix will be arranged into a vector.
% some random data
A = cell(3,1) ;
for i = 1:3
A{i} = round(rand(10,4)).*rand(10,4) ;
end
%%remove zeros
for i = 1:3
A{i}(A{i}==0) = [];
end

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!