How to add NaN in cell before last one

2 views (last 30 days)
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

Accepted Answer

KSSV
KSSV on 17 Dec 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

More Answers (0)

Categories

Find more on Numeric Types in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!