Compile multiple cells in a single vector including empty cells as zero elements.
1 次查看(过去 30 天)
显示 更早的评论
I have this as input
H = [] [] [5] [] [3]
and I want
H = [0 0 5 0 3]
as output.
Please help!
2 个评论
Michael Soskind
2020-5-6
I will provide a very naive method here. Maybe someone has a better solution.
H = {[],[],5,[],3}; % original array
for i = 1:size(H,1) % loop through rows (1)
for j = 1:size(H,2) % loop through columns (5)
if isempty(H{i,j})
H{i,j} = [0]; % if empty, replace the empty array with zero
end
end
end
H_new = cell2mat(H) % convert cell to matrix
You could use a temporary cell array if you do not want to modify the original cell array H.
回答(1 个)
Voss
2023-12-14
H = { [] [] [5] [] [3] } % original array
H(cellfun(@isempty,H)) = {0} % if empty, replace the empty array with zero
H = [H{:}] % convert cell to matrix
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!