Remove zero entries from cell arrays

4 ビュー (過去 30 日間)
Harold
Harold 2012 年 4 月 23 日
Suppose I have a m x n cell array. Some of these cells contain a 1 x 2 matrix of values other cells contain just a zero. I want to be able to delete all cells that contain zeros and have the cell array automatically re-size when finished. If you copy and past this in the command window you can see what I'm trying to accomplish. I've tried using ismember to look for the zeros but had no luck. I'm not very good when it comes to doing operations on cells. I need this to remain a cell array also. Thanks for any help.
points = {[-24.7697910000000,-15.8191235000000],0,[-12.6771670000000,20.4587480000000],0;0,[-20.6771670000000,-3.54125200000000],[-11.9803417500000,-14.5401785500000],0;0,[-20.6771670000000,-3.54125200000000],[4.32283300000000,-1.04125200000000],0;0,[13.0196582500000,-12.0401785500000],0,[-24.7697910000000,-15.8191235000000];[-11.9803417500000,-14.5401785500000],0,[4.32283300000000,-1.04125200000000],0;0,[-12.6771670000000,20.4587480000000],0,[13.0196582500000,-12.0401785500000];}

採用された回答

Sean de Wolski
Sean de Wolski 2012 年 4 月 23 日
points = {[-24.7697910000000,-15.8191235000000],0,[-12.6771670000000,20.4587480000000],0;0,[-20.6771670000000,-3.54125200000000],[-11.9803417500000,-14.5401785500000],0;0,[-20.6771670000000,-3.54125200000000],[4.32283300000000,-1.04125200000000],0;0,[13.0196582500000,-12.0401785500000],0,[-24.7697910000000,-15.8191235000000];[-11.9803417500000,-14.5401785500000],0,[4.32283300000000,-1.04125200000000],0;0,[-12.6771670000000,20.4587480000000],0,[13.0196582500000,-12.0401785500000];}
p2 = num2cell(points,2);
C = cellfun(@(c)cellfun(@(x)x(x~=0),c,'uni',false),p2,'uni',false);
for ii = numel(C):-1:1
Cc = C{ii};
C(ii) = {Cc(cellfun(@(c)~isempty(c),Cc))};
end
  1 件のコメント
Harold
Harold 2012 年 4 月 23 日
Thank you very much.

サインインしてコメントする。

その他の回答 (1 件)

Jan
Jan 2012 年 4 月 23 日
Another idea:
points = {[-24.7697910000000,-15.8191235000000],0, ...
[-12.6771670000000,20.4587480000000], 0; ...
0,[-20.6771670000000,-3.54125200000000], ...
[-11.9803417500000,-14.5401785500000],0; ...
0,[-20.6771670000000,-3.54125200000000], ...
[4.32283300000000,-1.04125200000000],0; ...
0,[13.0196582500000,-12.0401785500000],0, ...
[-24.7697910000000,-15.8191235000000]; ...
[-11.9803417500000,-14.5401785500000],0, ...
[4.32283300000000,-1.04125200000000],0; ...
0,[-12.6771670000000,20.4587480000000],0, ...
[13.0196582500000,-12.0401785500000]};
C = points(cellfun(@(x) ~isequal(x, 0), points));
"Automatically resize" is not unique: Should the number of rows or columns be adjusted?
C = reshape(C, size(points, 1), []); % For equal column length
  5 件のコメント
Harold
Harold 2012 年 4 月 25 日
This did not work. Order in each row is very important for what I need to do.
Jan
Jan 2012 年 4 月 25 日
Dear Harold, I'm sure you can solve this problem by your own using the commands TRANSPOSE, RESHAPE and/or PERMUTE. The main problem is solved by the CELLFUN call, and the finetuning is up to you.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by