MATLAB Answers

Making a group of maximum nearest elements.

1 view (last 30 days)
BHAGYALAKSHMI M
BHAGYALAKSHMI M on 11 Mar 2020
Commented: Guillaume on 17 Mar 2020
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

Guillaume
Guillaume on 11 Mar 2020
What's your definition of nearest elements?
BHAGYALAKSHMI M
BHAGYALAKSHMI M on 12 Mar 2020
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

Guillaume
Guillaume on 12 Mar 2020
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

Show 7 older comments
Guillaume
Guillaume on 17 Mar 2020
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
BHAGYALAKSHMI M
BHAGYALAKSHMI M on 17 Mar 2020
I want to take each group elements. Like:
group(1)=[elements]
group(2)=[elements] etc.
How can I get this?
Guillaume
Guillaume on 17 Mar 2020
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!