replace the values by nearest mean of the matrix
6 views (last 30 days)
Show older comments
how to replace the negative values of a 700 x 700 matrix by the nearest mean of values ?
2 Comments
Dyuman Joshi
on 25 Feb 2023
Please specify what you mean by "nearest mean of values".
For the given input, what should be the output?
y=randi(10,4,4)-5
the cyclist
on 25 Feb 2023
Edited: the cyclist
on 25 Feb 2023
Do you want the mean of the nearest neighbors? Does a neighbor along the diagonal count?
If so, what if the neighbors are also negative?
Answers (1)
John D'Errico
on 25 Feb 2023
Edited: John D'Errico
on 25 Feb 2023
Using my inpaint_nans....
y = randi(10,[7,7]) - 2
y =
0 5 0 8 1 0 2
6 5 7 -1 7 7 4
2 6 4 6 3 4 3
4 3 8 7 8 4 -1
0 -1 -1 7 0 0 1
5 1 3 -1 1 7 0
1 8 0 2 0 5 0
y(y < 0) = nan
y =
0 5 0 8 1 0 2
6 5 7 NaN 7 7 4
2 6 4 6 3 4 3
4 3 8 7 8 4 NaN
0 NaN NaN 7 0 0 1
5 1 3 NaN 1 7 0
1 8 0 2 0 5 0
yhat = inpaint_nans(y)
yhat =
0 5 0 8 1 0 2
6 5 7 8.3158 7 7 4
2 6 4 6 3 4 3
4 3 8 7 8 4 2.2857
0 1.5385 6.1538 7 0 0 1
5 1 3 3.6154 1 7 0
1 8 0 2 0 5 0
You can find inpaint_nans on the file exchange.
As pointed out in the comment, perhaps a better choice for this specific problem would have been method 4 in inpaint_nans, the springs method.
yhat = inpaint_nans(y,4)
yhat =
0 5 0 8 1 0 2
6 5 7 7 7 7 4
2 6 4 6 3 4 3
4 3 8 7 8 4 2.6667
0 2.2667 5.0667 7 0 0 1
5 1 3 3.25 1 7 0
1 8 0 2 0 5 0
This alternative scheme will tend to be more in keeping with the goal of any point being purely the average of its neighbors. (Note that itself has a problem when there are several points missing that are next to each other.)
4 Comments
the cyclist
on 25 Feb 2023
Thanks. I expect this is what @akash dey intended (subject to answers to the clarifying questions I asked in my comment).
John D'Errico
on 25 Feb 2023
Edited: John D'Errico
on 25 Feb 2023
Yes. I should have suggested that option first. As you can see, where there are singleton missing elements, it does return the average of the nearest missing direct neighbors. So we see there:
mean([3 4 1])
Another choice is the 5th method in there, which uses the average of the 8 nearest neighbors for a subtly different result. Any choice seems reasonable to me.
yhat = inpaint_nans(y,5)
yhat =
0 5 0 8 1 0 2
6 5 7 4.5 7 7 4
2 6 4 6 3 4 3
4 3 8 7 8 4 2.4
0 3.5423 4.3387 7 0 0 1
5 1 3 2.1673 1 7 0
1 8 0 2 0 5 0
I suppose one day I should revise the interface to make the options a bit more clean. Before I would do that though, I would want to implement a completely new method, since all of the existing methods are what I might think of as solutions of elliptic equations on a domain. My alternative method would involve some ideas I was playing with roughly 20 years ago, but they have been on the backshelf since. One day...
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!