Effacer les filtres
Effacer les filtres

extract row from a cell

10 vues (au cours des 30 derniers jours)
Alberto Acri
Alberto Acri le 17 Juin 2023
Réponse apportée : Voss le 17 Juin 2023
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.

Réponse acceptée

Voss
Voss le 17 Juin 2023
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'}

Plus de réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by