Merge the contents of neighboring non-zero cells

3 views (last 30 days)
LL
LL on 30 Nov 2021
Commented: LL on 2 Dec 2021
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

Answers (2)

Image Analyst
Image Analyst on 30 Nov 2021
Edited: Image Analyst on 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 Comments
LL
LL on 15 Dec 2021
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);
contenuto=zeros();
numero=1
for k=1:size(etichettaIm)
regione=ismember(etichettaIm,numero);
[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
cont=[CIRC_agg(r(1),c(1)),CIRC_agg(r(2),c(2)),CIRC_agg(r(3),c(3))];
contenuto=cat(1, cont{:});
for j=1:length(contenuto)
idrox=contenuto(find(contenuto==2));
perc_idrox=(length(idrox)*100)/length(contenuto);
%%%%%%%%%%%%
%%%EXTERNAL%%%
%%%%%%%%%%%%
if length(idrox)==0
WHI_ext_agg=0;
WHI_ext_agg=WHI_ext_agg+1;
%%%%%%%%%%%%
%%%INTERNAL%%%
%%%%%%%%%%%%
elseif perc_idrox>65
WHI_int_agg=0;
WHI_int_agg=WHI_int_agg+1;
%%%%%%%%%%%%%%
%%%BOUNDARIES%%%
%%%%%%%%%%%%%%
else
WHI_bound_agg=0;
WHI_bound_agg= WHI_bound_agg+1;
end
numero=numero+1;
%numero=numero+1;
if numero>numAgg
fprintf('Non ci sono più aggregati da controllare')
end
end
end
end
end

Sign in to comment.


Awais Saeed
Awais Saeed on 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]}
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 Comment
LL
LL on 30 Nov 2021
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!