Find the average between each pair of points in a matrix

Hello, I have a 61x61 random generated double matrix, I want to calculate the average between each point in a row of a column, and after going through all the rows in that column and calculating their corresponding averages, go to the next column.
If the average is >= 2 or <= -2 I would like to then set that data point to 1, otherwise set it to 0.
I managed to do this using for loops and if statements, but how would I do this using a vectorized approach?
I attatch what I have right now for reference.
Thanks.
Edit: Forgot to add, I would like to then, find the average between each point horizontally (what I stated above was vertically) so averaging between points in the 1st row and all columns, and then going to the 2nd row and so on. The average criteria is the same but if previously the data point is set to 1 and now it was calculated to be 0, keep it as 1, and if it was 0 and now was calculated as 1, then overwrite it to 1.
(I included two images to show what I mean by averaging horizontally and vertically.)

 Réponse acceptée

Joe Vinciguerra
Joe Vinciguerra le 11 Avr 2023
Modifié(e) : Joe Vinciguerra le 11 Avr 2023
I'm a little unclear on the specifics, but I think this is basically what you want:
z = randi([-5 5],61,61,'double')
zMeanV = movmean(z, [0 1], 1)
zMeanVH = movmean(zMeanV,[0 1], 2)
avgMap = or(zMeanVH >= 2, zMeanVH <= -2)
[edit]
After re-reading your post and looking closer at your code, it sounds like it should be more like this:
z = randi([-5 5],61,61,'double');
zMeanV = movmean(z, [0 1], 1);
zMeanV(end,:) = zMeanV(end-1,:);
avgMapV = or(zMeanV >= 2, zMeanV <= -2);
zMeanH = movmean(z,[0 1], 2);
zMeanH(:,end) = zMeanH(:, end-1);
avgMapH = or(zMeanH >= 2, zMeanH <= -2);
avgMap = or(avgMapV, avgMapH)

7 commentaires

This is a great answer Joe thanks a lot. Looking at the results again, I realized when the pair of numbers are equal and opposite (for example 5 and -5) then the gradient is 0 and data inserted is 0. Is there a way to make it so that when this scenario happens a 1 is inserted instead?
So you want the final result to be 1 if the mean is outside ±2 OR it it's equal to 0? Then you would insert this:
zMeanV(zMeanV==0) = 1;
and this:
zMeanH(zMeanH==0) = 1;
before calculating avgMapV and avgMapH, respectively.
JIAN-HONG YE ZHU
JIAN-HONG YE ZHU le 12 Avr 2023
Modifié(e) : JIAN-HONG YE ZHU le 13 Avr 2023
I mean if when calculating the mean, if the values used to calculate the mean are equal and opposite (i.e: 9/-9, 4/-4, 7/-7 etc), this would give a zero, so assign a one instead. I want to cover this specific scenario where the mean = 0.
I did manage to do it with for loops and if statements, not sure how you do it in a vectorized approach?
Again, this is how If you do it without FOR loops or IF statements:
z = randi([-5 5],61,61,'double');
zMeanV = movmean(z, [0 1], 1);
zMeanV(end,:) = zMeanV(end-1,:);
zMeanV(zMeanV==0) = 1;
avgMapV = or(zMeanV >= 2, zMeanV <= -2);
zMeanH = movmean(z,[0 1], 2);
zMeanH(:,end) = zMeanH(:, end-1);
zMeanH(zMeanH==0) = 1;
avgMapH = or(zMeanH >= 2, zMeanH <= -2);
avgMap = or(avgMapV, avgMapH)
avgMap = 61×61 logical array
Columns 1 through 49 0 1 1 1 0 1 0 0 1 0 0 0 1 1 1 1 1 0 1 1 1 1 0 1 1 1 0 1 0 1 1 1 0 0 1 1 1 0 0 0 1 0 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 1 0 1 1 1 0 1 1 1 1 0 1 1 0 0 0 0 1 1 0 0 0 1 1 1 0 1 1 0 0 0 0 0 1 1 1 0 1 0 0 0 0 1 0 0 1 1 1 1 0 1 0 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 0 0 1 0 0 1 0 0 1 1 1 1 1 1 1 1 1 1 0 0 1 1 0 1 1 0 0 1 1 1 1 1 1 0 0 1 0 0 1 1 1 1 1 0 0 0 1 1 1 1 1 1 0 0 1 1 1 1 1 0 0 1 0 0 1 1 1 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 0 1 1 1 1 1 0 0 1 1 1 1 0 0 1 0 0 0 1 1 1 1 0 0 1 1 1 1 0 1 0 1 1 1 0 1 0 0 1 1 0 0 1 0 0 1 1 0 1 1 0 1 1 0 1 0 1 1 1 1 1 0 1 1 1 0 1 1 1 1 1 0 0 1 1 0 0 1 1 0 0 1 1 1 0 1 1 0 1 0 1 0 1 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1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 0 1 1 0 1 0 0 0 0 1 1 1 1 1 1 1 1 1 0 1 1 1 0 0 1 1 1 1 1 1 0 0 0 1 0 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 0 0 1 0 1 1 1 1 0 0 0 1 1 1 0 1 1 1 1 1 1 0 1 1 1 0 0 1 1 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 1 1 0 0 1 1 1 1 1 1 1 0 1 1 0 0 0 1 1 1 0 0 1 1 1 0 0 0 0 1 1 1 1 1 1 0 1 0 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 0 1 0 1 1 0 0 1 1 0 0 1 1 0 1 1 1 1 0 0 1 1 1 1 1 1 1 0 0 1 1 0 0 1 1 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 1 0 0 1 0 1 1 0 1 0 0 1 1 1 1 0 1 0 1 1 1 1 1 1 1 0 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 0 0 1 1 1 1 1 1 1 0 1 1 1 0 1 1 0 1 1 0 0 1 1 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 1 1 0 1 1 1 1 1 1 0 1 1 1 1
dpb
dpb le 12 Avr 2023
Modifié(e) : dpb le 12 Avr 2023
z = randi([-5 5],8);
zMeanV = movmean(z, [0 1], 1);
zMeanH = movmean(zMeanV,[0 1], 2)
zMeanH = 8×8
2.0000 1.2500 0.2500 2.0000 0.5000 -0.2500 1.0000 1.0000 3.0000 0.5000 0.2500 1.2500 0.5000 -1.2500 -1.2500 -0.5000 0.5000 0 0.5000 0.2500 1.5000 0.5000 -1.0000 0 -0.7500 -0.7500 0.2500 1.7500 0.7500 0 0 -0.5000 -1.0000 0.5000 1.2500 2.5000 2.0000 -0.7500 -1.0000 -1.0000 -3.0000 -0.5000 -0.7500 0.2500 2.5000 0.7500 0 -1.0000 -3.2500 -3.0000 -2.2500 -0.5000 0.2500 1.5000 1.7500 -1.0000 -2.0000 -3.5000 -2.0000 -1.0000 -1.5000 1.0000 1.0000 -2.0000
zM=filter2(ones(2)/4,z);
zM(end,:)=2*zM(end,:); zM(:,end)=2*zM(:,end)
zM = 8×8
2.0000 1.2500 0.2500 2.0000 0.5000 -0.2500 1.0000 1.0000 3.0000 0.5000 0.2500 1.2500 0.5000 -1.2500 -1.2500 -0.5000 0.5000 0.0000 0.5000 0.2500 1.5000 0.5000 -1.0000 0.0000 -0.7500 -0.7500 0.2500 1.7500 0.7500 0.0000 -0.0000 -0.5000 -1.0000 0.5000 1.2500 2.5000 2.0000 -0.7500 -1.0000 -1.0000 -3.0000 -0.5000 -0.7500 0.2500 2.5000 0.7500 -0.0000 -1.0000 -3.2500 -3.0000 -2.2500 -0.5000 0.2500 1.5000 1.7500 -1.0000 -2.0000 -3.5000 -2.0000 -1.0000 -1.5000 1.0000 1.0000 -2.0000
all(round(zM,5)==round(zMeanH,5),'all')
ans = logical
1
There's no need to do both in sequence; that's the same result as averaging them together; as the above shows, they're the same if do normalize the last/padded row with filter2 (I forgot you actually do need to do the last element twice; it's padded both ways, not just one).
"...mean = 0, since it also happens if both values are 1, which is (1+1) / 2 = 0. "
Say what???? Since when is (1+1) / 2 anything but 1?
I think the idea of replacing the zero gradient arbitrarily with a positive value is misguided -- if it's realy a gradient and is a zero, then it is a zero and not a positive.
I'd have to be given more context of the real problem behind the specific request to be convinced otherwise....and what if it's -4 4 -1 ...? Then [- +]-->0 which goes to 1 and now you've introduced [1 -1] into the next location which undergoes the same translation...
If there is some real reason and the data are a random sampling scheme, then my suggestion would be to preprocess those locations with repetitive sampling to remove them by replacing one or the other of the two elements (or both) with a set that doesn't suffer the same weakness.
But, my first instinct remains to treat as find...
Sorry for that silly mistake. The context to the 'random matrix' (which I used to ask the question) is that the actual matrix is intended to be filled with points in a position of a map, and the value of each point is the height of that point in the map.
By calculating the average between points, then I could see whether it is feasible or not for a vehicle to go from two certain points, so if the average surpasses certain value, it means a vehicle could not drive through it, or it's a cliff (-4 4...).
That being said, I realized if I want this I should instead calculate the gradient instead which would be more relevant to this.
Thank you very much for your help I appreciate it
A 2D gradient() of the entire array definately seems to make more sense for the application. Cheers!

Connectez-vous pour commenter.

Plus de réponses (1)

dpb
dpb le 11 Avr 2023
Modifié(e) : dpb le 11 Avr 2023
I'd do it as
z=randi([-5 5],8)
z = 8×8
-4 -1 0 5 -5 -4 0 5 -1 -2 5 -1 -5 0 2 -4 1 -1 -1 -5 -4 -3 -1 3 2 5 -4 4 4 -1 -1 -2 4 1 -3 4 -3 2 1 2 -4 -5 0 5 -4 5 -1 1 4 0 0 -1 5 5 -4 1 -1 5 -2 5 -4 -4 1 -2
za=filter2(ones(2)/4,z) % averaging 2D filter with same size as input (zero padded)
za = 8×8
-2.0000 0.5000 2.2500 -1.5000 -3.5000 -0.5000 0.7500 0.2500 -0.7500 0.2500 -0.5000 -3.7500 -3.0000 -0.5000 -0.0000 -0.2500 1.7500 -0.2500 -1.5000 -0.2500 -1.0000 -1.5000 -0.2500 0.2500 3.0000 -0.2500 0.2500 2.2500 0.5000 0.2500 -0.0000 -0.0000 -1.0000 -1.7500 1.5000 0.5000 0.0000 1.7500 0.7500 0.7500 -1.2500 -1.2500 1.0000 1.2500 2.7500 1.2500 -0.7500 0.5000 2.0000 0.7500 0.5000 1.2500 0.5000 -0.5000 -1.0000 -0.2500 1.0000 0.7500 0.7500 0.2500 -2.0000 -0.7500 -0.2500 -0.5000
zv=filter2(ones(2)/4,z,'valid') % keep only non-padded elements
zv = 7×7
-2.0000 0.5000 2.2500 -1.5000 -3.5000 -0.5000 0.7500 -0.7500 0.2500 -0.5000 -3.7500 -3.0000 -0.5000 -0.0000 1.7500 -0.2500 -1.5000 -0.2500 -1.0000 -1.5000 -0.2500 3.0000 -0.2500 0.2500 2.2500 0.5000 0.2500 -0.0000 -1.0000 -1.7500 1.5000 0.5000 0.0000 1.7500 0.7500 -1.2500 -1.2500 1.0000 1.2500 2.7500 1.2500 -0.7500 2.0000 0.7500 0.5000 1.2500 0.5000 -0.5000 -1.0000
Alternatively, you can choose 'valid' and get the averages only where there is no zero padding. This then returns a filtered array that is in your case over averaging over two elements, one less in each direction.
The description of the scaling is confusing, however, but by the first description, it would be
za=double(abs(za)>=2) % scales to 0, 1 based on average (throws away sign???)
za = 8×8
1 0 1 0 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0

4 commentaires

Is filt2 built-in matlab? I found filt2 here: filt2 2D geospatial data filter - File Exchange - MATLAB Central (mathworks.com) but could not implement it how you showed above, it comes an error message mentioning not enough input arguments.
I did include another bracket za=filt2((ones(2)/4),z); but would not work regardless.
My bad, it's filter2; not sure why I dropped the trailing 'er'...it's in the base MATLAB product. I'll patch the above...
Thank you very much for your insight, I have one question, after reading through 2-D digital filter - MATLAB filter2 - MathWorks United Kingdom , 'filter2' has a syntax of Y = filter2(H,X), in this case, the matrix H is ones(2)/4. I do not understand how this matrix H is defined?
dpb
dpb le 12 Avr 2023
Modifié(e) : dpb le 12 Avr 2023
Ir's your 2x2 averaging kernel -- multiplies each set of four locations by 1/4 and adds which is the average over the four locations. As the doc also points out, filter is just conv (convolution) with the kernel rotated 90 degrees. Since yours is symmetric, it doesn't make any difference which.
You'll note that the first case, 'Same' is padded w/ zeros on bottom and right; those values are half of the average the the last two points. Sometimes (and what your code does if my glance-thru was correct) I'll multiply that last row/column by the ratio of the kernel size and the number of padded rows/columns(*). Here, of course, with a 2x2 kernel, it just has to pad the one row/column so the multiplier is 2/1-->2. You can see that result by inspection with the integer values it's easy to add and divide by four.
In the second example, 'Valid', the result is one row/column less than the original, but there's no padding and so the last row/column of the result are full 4-element averages.
Which to use and whether to reweight or not is all dependent upon what your needs are for the result; we can't know anything about that.
(*) If you do choose to normalize the last row/column, remember to only address the lower RH corner element once; otherwise you'll end up doing it twice!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Linear and Nonlinear Regression dans Centre d'aide et File Exchange

Produits

Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by