How to compare array's values with each other?

35 vues (au cours des 30 derniers jours)
phdcomputer Eng
phdcomputer Eng le 31 Juil 2019
Modifié(e) : phdcomputer Eng le 11 Août 2019
In an array (a) with indexes from 1 to m, I want to compare the values of this array one by one with each other, and if the distance (Difference) between two values is more than a value (z), for example, the difference between a(i) and a(j) at indexes i and j is more than z, I want to save these two indexes i and j and represent them in the output. I wrote these codes:
if abs(a(i)-a(j))> z
disp(i);
disp(j);
fprintf('result is between %10.6f and %10.6f',i,j);
end
but there is an error in if line:
Subscript indices must either be real positive integers or logicals.
How can I define indexes for matlab. Is a for loop (for i=1:m) needed for passing the array, If a loop is necessary, should I put fprintf out of the loop because it will repeat. For saving and representing the indexes i and j in the output, I'm looking for better functions besides disp or fprintf.

Réponse acceptée

Guillaume
Guillaume le 31 Juil 2019
It's unclear how you get your error if your i and j were just created with a for i = 1:m and for j=1:m. They're clearly something else for you to get that error.
Anyway, assuming a is a vector and assuming matlab>=R2016b, this is very straightforward:
distance = abs(a - a.')
will create a m x m matrix of the distance between a(i) and a(j) for all i and j.
finding the i and j of the elements for which distance is greater than z is also easy:
[i, j] = find(distance > z)
which you could store in a 2 column matrix if you wanted:
pairs = [i, j]
  5 commentaires
Guillaume
Guillaume le 9 Août 2019
"My aim was selecting the most discriminative features among all of the features of the data by sorting these distance values descendingly and then cut the greater values so just keep this count of features and discard the rest of them."
As I said, this is not my field. If most discriminative features is equivalent to pair of features with the largest hamming distance between them, then that part makes sense.
What I don't understand is what you do after, if you have hamming distance a(i) between feature V(m) and V(n), and hamming distance a(j) between feature V(x) and V(y), what does a(i)-a(j) mean (which is what you calculate with your distance)?
phdcomputer Eng
phdcomputer Eng le 11 Août 2019
Modifié(e) : phdcomputer Eng le 11 Août 2019
Thanks. I'm very grateful for your attention.
rst I tried to find the best point for cutting the best features by plotting the sorted distances (A) , but it's complicated because in some parts of the figure , values are changing gradually but in some points the decrease is suddenly, by the way sometimes I can't choose which points is better as threshold.for example in multiple points, the values have sudden drop.
as you said if we suppose a(i) and a(j) are disances.
a(i)-a(j) is the difference of two points in the plot(A) figure and we can put this condition that if the difference of two points in the figure is more than a computed value for example z , we keep the points i and j and we can cut i number of features.
my purpose of threshold is the point that the distance values are decreasing after that point impressive.
Thank you very much

Connectez-vous pour commenter.

Plus de réponses (1)

Jon
Jon le 31 Juil 2019
Staying close to what you have started here, you could put your code into a double loop, for example
% assign threshold
z = 10; % or what ever your threshold is
% find number of elements to loop through
N = length(a)
% preallocate array to hold results
% elements of D will be set to true (1) when
% a(i) and a(j) are further apart than threshold
D = zeros(N,N)
for i = 1:N
for j = 1:N
D(i,j)=abs(a(i)-a(j))> z
end
end
% display indices of elements whose absolute difference exceeds threshold, z
[idxI, idxJ] = find(D)
disp(idxI)
disp(idxJ)
  5 commentaires
Guillaume
Guillaume le 31 Juil 2019
For real matrix, I don't think there's any difference in performance between the two, so you can indeed use either.
However, since the OP never specified that the vectors were pure real, and since the original code would have worked with complex numbers, I used the plain transpose so as not to change the meaning of the distance formula.
By default, I tend to use .' so that the code works the same with real or complex numbers, when all is meant is changing the direction of a vector.
I'm not a mathematician, maybe it makes sense that the shorter ' is a conjugate tranpose. if the design had been up to me, I would have swapped the meaning of the two so that ' was a plain transpose and .' a conjugate transpose.
Jon
Jon le 31 Juil 2019
@Guillaume - Thanks for the explanation.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by