Making a group of maximum nearest elements.

If I have some points and a data. I need to find the all the nearest elements of points. How can I do that ? Please help me.

2 Comments

What's your definition of nearest elements?
The nearest elements that are close to each point. I atttached a data here.
Suppose I have some elements A= [1,4,2,6,9,0,4.2,5.6,....] and points
B=[0.1,4.1,3.......]. Calculating and comparing the distance of these and based on that grouping.
Taking point 1 from A checking is this element is close to 0.1 or 4.1. It is close to 1 more than 4 so
G1=[1,0.1].
Taking point 4. It is close to 4.1, so
G2=[4,4.1].
2 is close to 3, so
G3=[2,3].
6 is close to 4.1 compared to others, so
G2=[4,4.1].
The process repeats until no elements in A.
How can I do that? Please help me.

Sign in to comment.

 Accepted Answer

Be aware that the following creates a temporary matrix of size numel(A) x numel(B), so if both vectors are very large you may run out of memory:
%for row vectors:
%example data
A = [1,4,2,6,9,0,4.2,5.6];
B = [0.1,4.1,3];
assert(isrow(A) && isrow(B), 'Inputs must be row vectors');
[~, groupA] = min(abs(A - B.'), [], 1)
%for column vectors:
%demo data
A = A.'; B = B.';
assert(iscolumn(A) && iscolumn(B), 'Inputs must be column vectors')
[~, groupA] = min(abs(A.' - B), [], 1)
I'm not sure how this should be applied to your example datasheet. Looking at what it contains, you should import that excel file in a table, in which case you should store the group as another variable of the table so you can then use aggregation functions such as groupsummary.

10 Comments

Thank you sir.
I am not getting the output,there was some errors.
The following is my points.
B=
Columns 1 through 10
44.8481 1.7293 1.4514 0.6036 199.6823 29.9475 35.4337 6.5257 3.2450 0.9861
39.3333 1.8333 1.6500 0.6500 492.5000 26.8333 32.1667 6.5333 2.6167 0.6617
57.4286 1.7143 2.0714 0.9286 198.0000 53.1429 93.4286 5.8000 2.9000 1.0114
47.0000 2.0000 9.0250 4.5750 207.0000 62.2500 181.2500 6.7500 2.7000 1.1750
57.0000 2.0000 1.4000 0.7000 470.0000 62.0000 88.0000 5.6000 2.5000 0.8000
23.3333 2.0000 3.4333 1.4667 287.3333 67.3333 139.3333 5.6000 2.4667 0.7667
11.0000 1.3333 0.8333 0.1667 392.0000 25.0000 66.3333 7.0667 3.5667 1.0667
65.0000 2.0000 0.7000 0.1000 392.0000 20.0000 30.0000 5.3000 2.8000 1.1000
54.0000 1.0000 22.6000 11.4000 558.0000 30.0000 37.0000 7.8000 3.4000 0.8000
34.0000 2.0000 3.7000 2.1000 490.0000 115.0000 91.0000 6.5000 2.8000 0.7000
70.0000 2.0000 2.7000 1.2000 365.0000 62.0000 55.0000 6.0000 2.4000 0.6000
54.3333 2.0000 5.1000 2.2000 223.3333 60.3333 149.0000 6.9000 2.8333 0.6333
Column 11
1.3867
1.5000
1.2857
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
How can I find nearest points of this from my dataset? Please help me.
I am getting an error
"Error using wn (line 25)
Inputs must be row vectors"
How can I get the result?
Well, yes I explicitly wrote the check that the inputs were vectors. Why is the input a matrix? What is the desired result when the input is a matrix? Are both A and B matrices? What is to be checked againt what? etc.
You need to give a lot more explanation as to what exactly you want. An explanation of what the inputs represent would also help.
If I take the unique values, then I get some B like--
B=0.1000
0.1667
0.6000
0.6036
0.6333
0.6500
0.6617
0.7000
0.7667
0.8000
0.8333
0.9286
0.9861
1.0000
1.0114
1.0667
1.1000
1.1750
1.2000
1.2857
1.3333
1.3867
1.4000
1.4514
1.4667
1.5000
1.6500
1.7143
1.7293
1.8333
2.0000
2.0714
2.1000
2.2000
2.4000
2.4667
2.5000
2.6167
2.7000
2.8000
2.8333
2.9000
3.2450
3.4000
3.4333
3.5667
3.7000
4.5750
5.1000
5.3000
5.6000
5.6000
5.8000
6.0000
6.5000
6.5257
6.5333
6.7500
6.9000
7.0667
7.8000
9.0250
11.0000
11.4000
20.0000
22.6000
23.3333
25.0000
26.8333
29.9475
30.0000
32.1667
34.0000
35.4337
37.0000
39.3333
44.8481
47.0000
53.1429
54.0000
54.3333
55.0000
57.0000
57.4286
60.3333
62.0000
62.2500
65.0000
66.3333
67.3333
70.0000
88.0000
91.0000
93.4286
115.0000
139.3333
149.0000
181.2500
198.0000
199.6823
207.0000
223.3333
287.3333
365.0000
392.0000
470.0000
490.0000
492.5000
558.0000
Choosing this as B how can I find all neighbors of these points.
A is a data, B is some points suppose B contains 50 points then we will get 50 groups. Suppose the data contains some points like: {1,0.3,4,7,9,23,55, etc} (a matrix). B contains:{ 0.4,10,44,60,etc}.
Check the whole data, the first point is '1' in the data. This point is close to 0.4 compared to the other points in B. Next is 0.3, this also close to 0.4 compared to others. So we got a group like 0.4,0.3,1. This process continues when all the elements in the data checked. At last, we get some groups/clusters. Like
C1=[1,0.4,0.3,4]
Then 7 (from data) that is close to 10 then
C2=[7,10,9,23]
55 close to 60 compared to others
C3=[55,60]
The 44 is remaing, this is close to C3 elements so
C3=[44,55,60].
How can get some groups that contains(the groups) all the elements inthe dataset ?
Please help me.
Is there any method to find this?
If B is a vector and A is a matrix and you want to find the index of the element of B each element of the A matrix is closest to (= your group number), then:
assert(isvector(B), 'B must be a vector');
[~, groupnumber] = min(abs(reshape(A, 1, []) - B(:)), [], 1);
groupnumber = reshape(groupnumber, size(A));
This gives you a matrix the same size as A indicating which group each elements falls in.
Be aware that this temporarily creates a numel(A) x numel(B) matrix. For very large matrices, you may run out of memory.
Thank you sir. From this how can I get Group1 elements?
A(groupnumber == 1)
is all the elements of group 1. I'm not sure what you're planning to do afterwards. Be aware that creating a matrix for each group is not a good approach. There are plenty of matlab functions that will let you apply the same algorithm to all the groups at once, as long as everything is kept together in one matrix.
For example, if you want to calculate the mean of each group in A:
groupmean = groupsummary(A(:), groupnumber(:), 'mean'); %calculate the mean of each group of A
I want to take each group elements. Like:
group(1)=[elements]
group(2)=[elements] etc.
How can I get this?
As I've said, this is typically a bad idea. I was asking about what you want to do after that. It is very likely that whatever it is, it will be much easier if you don't do this.
Now if you really insist:
group = accumarray(a(:), groupnumber(:), [], @(v) {v});
or
group = splitapply(@(v) {v}, a(:), groupnumber(:));

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!