fill empty spaces in a cell array
15 次查看(过去 30 天)
显示 更早的评论
Hi all, I have a 2000x2 cell array.
When I go down I see some rows have [] [] empty. What way can I do it so that the empty 2 cells in the array are always replaced by the next immediate rows underneath it?
I hope this was easy to understand:
Eg,
123341 23.123
324523 34.245
[] []
234536 23.452
Then my output would be:
123341 23.123
324523 34.245
234536 23.452
234536 23.452
0 个评论
采纳的回答
Geoff
2012-4-3
Here's a clunky solution:
function [A] = FillEmptyRows( A )
while 1
emptyCells = cellfun(@(x) isempty(x), A);
emptyRows = all(emptyCells,2);
if ~any(emptyRows)
break;
end
nextRows = circshift(emptyRows,1);
A(emptyRows,:) = A(nextRows,:);
end
end
It works by identifying rows that are entirely empty (so a row with only one empty entry won't be replaced), and replaces that with the next row.
For simplicity, I use circshift to use the logical index on the next row, but that means if your last row is empty it will be replaced by the first row.
If you have multiple consecutive empty rows, the loop will go round several times but you'll always end up with a filled result. Don't call it if EVERY row is empty.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Identification 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!