Merge the contents of neighboring non-zero cells
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (2)
Image Analyst
le 30 Nov 2021
Modifié(e) : Image Analyst
le 30 Nov 2021
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
10 commentaires
Awais Saeed
le 30 Nov 2021
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)
Voir également
Catégories
En savoir plus sur Microscopy dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!