Effacer les filtres
Effacer les filtres

How to add NaN in cell before last one

1 vue (au cours des 30 derniers jours)
haitham qutaiba
haitham qutaiba le 17 Déc 2019
Commenté : haitham qutaiba le 17 Déc 2019
Dear all,
i have a cell with different sizes
a{1} = [1 2 3 4 5 10];
a{2} = [2 2 4 5 10];
a{3} = [8 2 10];
a{4} = [8 2 9 9 2 1 3 10];
one row, 4 columns,
number 10 represent (label or number of class) of each one,
v = cellfun('size',a,2);
r = numel(a);
M = NaN(r,max(v));
for k = 1:r
M(k,1:v(k)) = a{k};
end
this for loop to find max and make the cell equal before change it to matrix, the problem is NaN added after last number which 10,
M =
1 2 3 4 5 10 NaN NaN
2 2 4 5 10 NaN NaN NaN
8 2 10 NaN NaN NaN NaN NaN
8 2 9 9 2 1 3 10
is there a way to add NaN before label 10 and make it equal as well?
thanks in advnace

Réponse acceptée

KSSV
KSSV le 17 Déc 2019
a{1} = [1 2 3 4 5 10];
a{2} = [2 2 4 5 10];
a{3} = [8 2 10];
a{4} = [8 2 9 9 2 1 3 10];
v = cellfun('size',a,2);
r = numel(a);
M = NaN(r,max(v));
for k = 1:r
M(k,1:v(k)-1) = a{k}(1:end-1);
M(k,end) = a{k}(end) ;
end

Plus de réponses (0)

Catégories

En savoir plus sur Numeric Types dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by