从具有一些空单元格而不考虑空单元格的单元格创建新矩阵 [英] Creating new matrix from cell with some empty cells disregarding empty cells

查看:92
本文介绍了从具有一些空单元格而不考虑空单元格的单元格创建新矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Matlab中有一个名为: elem

I have in Matlab a cell named: elem

 [36 29]
 []
 [30 29]
 [30 18]
 []
 [31 29]
 []
 []
 [8 9]
 [32 30]

和一个名为 conn

1   2
1   3
1   4
1   5
2   3
2   4
2   5
3   4
3   5
4   5

,我想制作2个新矩阵,其中将仅包含与 elem 的非空单元格相对应的元素,而无需使用for循环. 例如,正确的结果将是:

and i want to make 2 new matrices which will contain only the elements that correspond to non-empty cells of elem without using a for loop. For example the correct result would be:

29  36
29  30
18  30
29  31
8   9
30  32

和:

1   2
1   4
1   5
2   4
3   5
4   5

任何帮助将不胜感激.

推荐答案

inds = ~cellfun('isempty', elem);    %// NOTE: faster than anonymous function
conn = conn(inds,:);
elem = elem(inds);                   %// (preservative)

inds = cellfun('isempty', elem);     %// NOTE: faster than anonymous function
conn(inds,:) = [];
elem(inds  ) = [];                   %// (destructive)

inds = cellfun(@(x)isequal(x,[]), elem)  %// NOTE: stricter; evaluates to false 
conn = conn(inds,:);                     %// when the 'empties' are '' or {}
elem = elem(inds);                       %// or struct([])

inds = cellfun(@(x)isequal(x,[]), elem)  %// "
conn(inds,:) = [];
elem(inds  ) = [];

inds = cellfun(@numel, elem)==2    %// NOTE: even stricter; only evaluates to 
conn = conn(inds,:);               %// true when there are exactly 2 elements 
elem = elem(inds);                 %// in the entry

inds = cellfun(@numel, elem)==2    %// " 
conn(inds,:) = [];
elem(inds  ) = [];

或(如果您只对elem感兴趣)

or (if you're just interested in elem)

elem = cell2mat(elem)

elem = cat(1,elem{:})  %// NOTE: probably the fastest of them all

这篇关于从具有一些空单元格而不考虑空单元格的单元格创建新矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆