Merge the contents of neighboring non-zero cells
3 次查看(过去 30 天)
显示 更早的评论
Hi i have only one cell array and, what i would like to do is to merge the contents of neighboring non-zero cells.
So, in the figure all the contents of the groups with the blue outline will be "merged" into one cell.
i.e. in the final cell array in the col n.8 row n.6 it will be [9;9;9;9;1;1;.....9;2;1;1;2;1;......2;1;9;2;9;9;.....].
Thanks
0 个评论
回答(2 个)
Image Analyst
2021-11-30
编辑:Image Analyst
2021-11-30
Why do you need to to this? I'm thinking you think you need to do this to accomplish some task, but you really don't. That's why I was wanting to know the use case -- what you're going to do once you have this.
It's possible but not easy. You'd have to get a map of what cells are non-zero, then label that map with bwlabel() to determine what cells are contiguous. Then you could identify the upper left cell (if that's what you want) of each region, and replace it with the concatenation of all the other cells inside that contiguous region. Then replace the other cells with 0 (I guess).
So it is possible but a bit of work so I need to know if it's really necessary.
Here's start, but let's hope you don't really need this:
[rows, columns] = size(ca); % Get size of the cell array variable, ca.
binaryImage = true(rows, columns);
for col = 1 : columns
for row = 1 : rows
cellContents = ca{row, col};
if isequal(cellContents, 0)
binaryImage(row, col) = false;
end
end
end
[labeledImage, numRegions] = bwlabel(binaryImage);
for k = 1 : numRegions
thisRegion = ismember(labeledImage, k);
[r, c] = find(thisRegion);
% Collect contents of all cells in this region and concatenate them.
% You do this. Loop over all cells defined by r and c getting contents and concatenating them.
% Then store result into ca(r(1), c(1));
% You do this. Something like ca{r(1), c(1)} = concatenatedResults.
% Other cells set to zero if you want.
end
Awais Saeed
2021-11-30
Remove the cells that has 0 in it.
c = {25,0,'anyString','aChar',[12 5 48 7];0,0,'anyString','bChar',[10 01]}
nonzero_indices = cellfun(@(x) ~isequal(x, 0), c);
M = c(nonzero_indices)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Microscopy 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!