# Average Similar Data Points Using a Tolerance

This example shows how to use `uniquetol` to find the average `z`-coordinate of 3-D points that have similar (within tolerance) `x` and `y` coordinates.

Use random points picked from the `peaks` function in the domain $\left[-3,3\right]×\left[-3,3\right]$ as the data set. Add a small amount of noise to the data.

```xy = rand(10000,2)*6-3; z = peaks(xy(:,1),xy(:,2)) + 0.5-rand(10000,1); A = [xy z]; plot3(A(:,1), A(:,2), A(:,3), '.') view(-28,32)``` Find points that have similar `x` and `y` coordinates using `uniquetol` with these options:

• Specify `ByRows` as `true`, since the rows of `A` contain the point coordinates.

• Specify `OutputAllIndices` as `true` to return the indices for all points that are within tolerance of each other.

• Specify `DataScale` as `[1 1 Inf]` to use an absolute tolerance for the `x` and `y` coordinates, while ignoring the `z`-coordinate.

```DS = [1 1 Inf]; [C,ia] = uniquetol(A, 0.3, 'ByRows', true, ... 'OutputAllIndices', true, 'DataScale', DS);```

Average each group of points that are within tolerance (including the `z`-coordinates), producing a reduced data set that still holds the general shape of the original data.

```for k = 1:length(ia) aveA(k,:) = mean(A(ia{k},:),1); end```

Plot the resulting averaged-out points on top of the original data.

```hold on plot3(aveA(:,1), aveA(:,2), aveA(:,3), '.r', 'MarkerSize', 15)```
{"ft_lang_label":"__label__en","ft_lang_prob":0.6899854,"math_prob":0.99773175,"size":1304,"snap":"2023-40-2023-50","text_gpt3_token_len":380,"char_repetition_ratio":0.12538461,"word_repetition_ratio":0.010204081,"special_character_ratio":0.297546,"punctuation_ratio":0.22483222,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99427795,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,3,null,3,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-10-04T00:46:09Z\",\"WARC-Record-ID\":\"<urn:uuid:4ed7bee6-243e-457b-9bd9-057a9991602c>\",\"Content-Length\":\"73982\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:6b92198c-21f5-4333-a001-7fbd0f844306>\",\"WARC-Concurrent-To\":\"<urn:uuid:ba566128-9ecd-4dfe-afd4-2eb5151040e7>\",\"WARC-IP-Address\":\"23.34.160.82\",\"WARC-Target-URI\":\"https://it.mathworks.com/help/matlab/matlab_prog/average-similar-data-points-using-a-tolerance.html\",\"WARC-Payload-Digest\":\"sha1:6CKVLNMIBRM3Z7XQE5HUP4ZQTWK5DBSU\",\"WARC-Block-Digest\":\"sha1:FATQJE2CZ3HHRVVH72VQWMUH4MW54W6M\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233511284.37_warc_CC-MAIN-20231003224357-20231004014357-00846.warc.gz\"}"}