MATLAB: How to compare array’s values with each other

arraysindexingMATLABmatrixmatrix array

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.

Best Answer

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]