How to add NaN in cell before last one

조회 수: 2 (최근 30일)
haitham qutaiba
haitham qutaiba 2019년 12월 17일
댓글: haitham qutaiba 2019년 12월 17일
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

채택된 답변

KSSV
KSSV 2019년 12월 17일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by