extract row from a cell

10 次查看(过去 30 天)
Alberto Acri
Alberto Acri 2023-6-17
回答: Voss 2023-6-17
Hi! I generated a cell similar to this one:
a = {'home'};
b = {'ball' , 'cake' , 'ice'};
c = {'car','moto'};
d = {'money','supercar','toys'};
e = cell(4,0);
e(1,1:numel(a)) = a;
e(2,1:numel(b)) = b;
e(3,1:numel(c)) = c;
e(4,1:numel(d)) = d
I want to extract from cell "e" the first row consisting of three values.
In the case above, the second row of cell "e" consists of three values, one per column. I want to extract this second row.

采纳的回答

Voss
Voss 2023-6-17
a = {'home'};
b = {'ball' , 'cake' , 'ice'};
c = {'car','moto'};
d = {'money','supercar','toys'};
e = cell(4,0);
e(1,1:numel(a)) = a;
e(2,1:numel(b)) = b;
e(3,1:numel(c)) = c;
e(4,1:numel(d)) = d;
% construct a logical matrix the same size as e saying whether each
% corresponding cell of e is non-empty:
e_nonempty = ~cellfun(@isempty,e)
e_nonempty = 4×3 logical array
1 0 0 1 1 1 1 1 0 1 1 1
% find the first row of e that has three non-empty cells:
row = find(sum(e_nonempty,2) == 3,1)
row = 2
% extract that row of e:
result = e(row,:)
result = 1×3 cell array
{'ball'} {'cake'} {'ice'}

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by