Merge the contents of neighboring non-zero cells

4 ビュー (過去 30 日間)
LL 2021 年 11 月 30 日
コメント済み: LL 2021 年 12 月 2 日
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;.....].

回答 (2 件)

Image Analyst
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;
[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.
  10 件のコメント
LL 2021 年 12 月 15 日
Hi @Image Analyst, I thought I could check the LabelIm array, especially for the first loop I initialize number = 1 then I check all cells = 1, find their rows and columns and get their value from CIRC_agg when I have all the contents of the contingent cells I have concatenated them and studied the contents to verify if the aggregates are internal, external or boundary of hydroxypatite.
I show you the code.
[etichettaIm, numAgg] = bwlabel(binaryIm);
for k=1:size(etichettaIm)
[r, c]=find(regione);
for y=1:r
for i=1:c
%%%i would like to cycling but i don't know how i tried with cont=[CIRC_agg(r(y),c(i))]; without results
contenuto=cat(1, cont{:});
for j=1:length(contenuto)
if length(idrox)==0
elseif perc_idrox>65
WHI_bound_agg= WHI_bound_agg+1;
if numero>numAgg
fprintf('Non ci sono più aggregati da controllare')


Awais Saeed
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]}
c = 2×5 cell array
{[25]} {[0]} {'anyString'} {'aChar'} {[12 5 48 7]} {[ 0]} {[0]} {'anyString'} {'bChar'} {[ 10 1]}
nonzero_indices = cellfun(@(x) ~isequal(x, 0), c);
M = c(nonzero_indices)
M = 7×1 cell array
{[ 25]} {'anyString'} {'anyString'} {'aChar' } {'bChar' } {[12 5 48 7]} {[ 10 1]}
  1 件のコメント
LL 2021 年 11 月 30 日
Sorry @Awais Saeed, I need that the final array has the same size of the initial one.
So when i find a cell non-zero, i need to check around this cell to find and merge the contents of the other non-zero cells and put inside these one the 0 or NaN.


Community Treasure Hunt

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

Start Hunting!

Translated by