Remove zeros from cell
조회 수: 15 (최근 30일)
이전 댓글 표시
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
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
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) = {[]};
댓글 수: 0
추가 답변 (1개)
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
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!