Compare pairs of elements in an array

16 views (last 30 days)
Francisco
Francisco on 30 May 2015
Answered: Star Strider on 30 May 2015
I need to compare pairs of elements of a matrix (by row), but also some operations once compared. For example, I have the following matrix.
A=[300 105 110 100;
100 140 300 105];
10 140 300 200];
10 25 300 310];
I need to compare values within a row and see which are close to each other, if the values are within a range of +/- 20 between them, I keep them, if not that is filled with NaN. For example the first row got to the last three are in the range and the first not, then my new row is
B(1)=[NaN 105 110 100];
and the second one:
B(2)=[100 NaN Nan 105];
In case none other approaches, simply fill with NaN the row. As is the case of the third row.
B(3)=[NaN NaN Nan NaN];
The last case is that if I have two cores (two pairs close and the other two pairs close but not the first) and row 4, fill with NaN.
B(4)=[NaN NaN Nan NaN];
I hope you can help me, I have not been able to solve completely. I also hope I explained well.
Greetings and thank you in advance.
Francisco

Answers (2)

Image Analyst
Image Analyst on 30 May 2015
Here's an easy to understand, intuitive brute force way:
A=[300 105 110 100;
100 140 300 105;
10 140 300 200;
10 25 300 310];
maxAllowableDistance = 20;
[rows, columns] = size(A);
B = A; % Initialize
for row = 1 : rows
for col = 1 : columns
distances = abs(A(row, col) - A(row,:))
% Remove distance of element to itself.
distances(col) = nan;
% Find the next closest element.
minDistance = min(distances)
% If it's too far from all the others
% set the value of B to nan.
if minDistance > maxAllowableDistance
B(row, col) = nan;
end
end
end
% Print to command window
B
Results:
B =
NaN 105 110 100
100 NaN NaN 105
NaN NaN NaN NaN
10 25 300 310
  5 Comments
Francisco
Francisco on 30 May 2015
Yes, probably speak about "pairs" is incorrect. Sorry for that.
Basically, for each row, the four values should be equal (or almost equal), as they are not equal, I need to select "the correct" according to some criterion. Therefore, I assume that when at least two values are close within some range (difference of +/- 20), they are correct. If 3 o 4 are close, much better. On the other hand, if I have two pairs close and the other two are close between them too, but far from the first two (e.g. row 4, 10-25 and 300-310) I can not know which are the correct, so I prefer to discard all.
I hope you can understand me now. Thank again for your help.
Image Analyst
Image Analyst on 30 May 2015
I don't know - that's getting kind of specialized. You might just have to check all 6 possible pairs one at a time and have a bunch of nested if statements to handle the "two close pairs but not close to the other pair" case.

Sign in to comment.


Star Strider
Star Strider on 30 May 2015
I thought about your Question and admit that I still do not understand it. There seems to be no clear pattern, and the ‘examples’ you give are inconsistent.
If you are checking for out-of-range numbers (perhaps deviating from the mean by some quantity), that is relatively easy to do. The usual method is to choose some multiple of the standard deviation (use the std function) and add or subtract it from the mean (use the mean function) to get the threshold for inclusion to keep the values you want.
I would do something like this:
A=[300 105 110 100;
100 140 300 105;
10 140 300 200;
10 25 300 310];
Asd = std(A,[],2); % Standard Deviation (Row-wise)
Amn = mean(A,2); % Mean (Row-wise)
Fct = 1; % ‘Factor’ Scaling ‘Asd’
Aex = [bsxfun(@lt, A, Amn-Fct*Asd) | bsxfun(@gt, A, Amn+Fct*Asd)];
B = A;
B(Aex) = NaN;
Here, Fct is the scaling factor for the standard deviation that multiplies it according to some criterion. (The usual value is the normal distribution z-value that includes 95% of the observations, about 1.96. I arbitrarily set it to 1 here.) This produces ‘Aex’, a logical matrix of positions to exclude (assign NaN). The assignment after Aex does that.
This does not produce the matrix you specified (as best as I can understand your examples), but it has the virtue of being statistically ‘logical’ as well as consistent.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!