How to search/average adjacent cell in matrix?
5 views (last 30 days)
Show older comments
Hi, I just started to learn Matlab and having a trouble to figure out my problem.
I defined a matrix as,
A = [0 1 2 0 2 0]
What I want to do is, 1) searching any cells with non-zero value regardless of location, 2) check whether it has any adjacent cell or not and 3) get the average of each chosen cell. If cell have an adjacent cell, then add value of each cell and return as average. If cell do not have any adjacent cell, then return the value of single cell as representative.
In my matrix case 'A', it should detect '1','2' at cell 2 and 3, and the average of this combination returns as '1.5 (3/2=1.5 as average)' and can be defined as 'A1' or something. Next, it also should detect '2' at cell 5 and the average of this combination returns as '2 (2/1=2 as average)' and can be defined as 'A2'.
It can be returned as single value, or it will be good to save as matrix 'B'
B = [1.5 2];
and the size of matrix 'B' should be flexible depend on the shape of matrix 'A'.
I'm hoping to extend my matrix size up to over 1000, but if I can get one Matlab code for this, it will be simple to expand to the larger size. Thanks.
Ben
1 Comment
Stephen23
on 18 Feb 2015
Edited: Stephen23
on 18 Feb 2015
You should learn the correct terms in MATLAB, otherwise it will be very confusing to talk about...
In MATLAB the term "cell" has a very special meaning, related to a particular class of data called cell arrays . Each individual location of a numeric array, like you have defined for A, is called an "element" (for example see element indexing ).
Accepted Answer
Stephen23
on 18 Feb 2015
Edited: Stephen23
on 18 Feb 2015
Try this:
>> A = [0 1 2 0 2 0];
>> X = diff(A~=0); % locations of all non-zero values
>> B = find(X>0)+1; % indices of begin of non-zeros groups
>> E = find(X<0); % indices of end of non-zeros groups
>> arrayfun(@(b,e)mean(A(b:e)), B, E) % mean of each group
ans = 1.5000 2.0000
3 Comments
Stephen23
on 19 Feb 2015
Edited: Stephen23
on 19 Feb 2015
As I wrote in my comment to your original question, there are no cell arrays involved in your question, only numeric arrays , so the correct term is element, not cell. Learning the correct terminology makes it easier for you too!
The code assumes that there is a leading and trailing zero. You can resolve this by using this code instead, which concatenates zeros on both ends of A:
>> X = diff([0,A,0]~=0);
>> B = find(X>0);
>> E = find(X<0)-1;
The rest is the same. The code you are asking about diff(A~=0) is made up of two parts, here in order of operation:
- the boolean inequality operator, which compares two arrays and returns true for corresponding elements if they have different values. This is the inverse of the equality operator, which checks if elements are identical.
- diff , which calculates the difference between adjacent elements.
In this case I find all non-zero elements using A~=0, and then can locate the start and end of these groups using the diff operation.
More Answers (1)
Image Analyst
on 19 Feb 2015
If you have a 2D array , it can be done with a single line of code that calls conv2() twice. Let me know if that's the case.
3 Comments
Image Analyst
on 2 Sep 2021
Just use conv2.
kernel = ones(3);
kernel(2,2) = 0; % Now kernel is a square ring of the 8 pixels surrounding the center pixel.
mask = grayImage == 0;
% Sum up the gray levels surrounding each pixel.
sumImage = conv2(grayImage, kernel, 'same');
% Count the number of non-zero pixels around each pixel.
countImage = conv2(mask, kernel, 'same')
% Compute the average image
averageImage = uint8(double(sumImage) ./ double(countImage));
% Replace zeros with the average
grayImage(mask) = averageImage(mask);
If you have larger, irregularly shaped regions of zeros, consider using
grayImage = regionfill(grayImage, mask);
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!