replace the values by nearest mean of the matrix

how to replace the negative values of a 700 x 700 matrix by the nearest mean of values ?

2 Kommentare

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
y = 4×4
2 1 5 -3 -2 5 -1 -2 -2 -4 -3 1 3 -3 3 0
the cyclist
the cyclist am 25 Feb. 2023
Bearbeitet: the cyclist am 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?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

John D'Errico
John D'Errico am 25 Feb. 2023
Bearbeitet: John D'Errico am 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 Kommentare

@John D'Errico, I'm confused by the element of yhat that is 8.3158, which larger than every other element of the input array.
Can you give me some insight into what is happening there?
John D'Errico
John D'Errico am 25 Feb. 2023
Bearbeitet: John D'Errico am 25 Feb. 2023
There are different options in inpaint_nans. The default one uses a method that tries to be as smoothly consistent with the data. And if the data would imply the function passes through a local maximum at a missing point, then the interpolated point would be greater than its neighbors. So you can think of the default method as trying to pass a thin flexible plate through the surface, where only the missing points are allowed to move. The result is the plate with minimal potential energy due to bending.
Perhaps I should have suggested use of method 4 in that code, which uses what I called a spring metaphor instead.
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
The spring metaphor pretends that every NaN element is attached to its neighbors by springs. It then tries (using linear algebra to solve the problem) to minimize the total potential energy stored in the entire set of springs. That scheme will not allow points to fall outside of the range of the data, since doing so would naturally create a system with higher potential energy.
Thanks. I expect this is what @akash dey intended (subject to answers to the clarifying questions I asked in my comment).
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])
ans = 2.6667
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...

Melden Sie sich an, um zu kommentieren.

Gefragt:

am 25 Feb. 2023

Bearbeitet:

am 25 Feb. 2023

Community Treasure Hunt

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

Start Hunting!

Translated by