필터 지우기
필터 지우기

Remove zeros from cell

조회 수: 14 (최근 30일)
Adam
Adam 2017년 10월 27일
답변: Star Strider 2017년 10월 27일
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
Reza Bonyadi
Reza Bonyadi 2017년 10월 27일
mydata{i} is a two dimensional matrix. What would be the result if you remove zeros from that? What would you replace them by?

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2017년 10월 27일
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) = {[]};

추가 답변 (1개)

KSSV
KSSV 2017년 10월 27일
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

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by