for loop in an array to substract the values of the array and find a specific value

I have an array of 200 values. This is the operation I need to program:
The value in the position X minus the value in the position Y EQUALS 0.55. Therefore, I am using a for loop and If statement.
The problem is that I know neither value nor the position of each value. I guess both of them are in the first 50 values of the array. So, how can I code the operation below n times (k-1, k-2, k-3 ...) until I got this number, and disp the 'Here'?
I;
for k =length (I)
x=I(k)-I %Take last value and substract the first value of I, second, k times to the last value.
if x==0.55
disp 'Here'
else
disp 'Not here'
end
end

 Accepted Answer

If you are trying to find the indices X and Y for which I(X) - I(Y) is equal to a given values, this is easily done without a loop with:
[X, Y] = find(I - I.' == seachvalue); %I must be a vector
This will returrn all the XY pairs that match.

4 Comments

Hi Guillaume,
Thanks for your time, but still I cannot get the XY pairs with this approach.
There is no value for X and Y.
"There is no value for X and Y."
Then there's no values in your array whose difference is exactly equal to the search value.
As pointed out by Image Analyst, if you're comparing floating points values that have been calculated differently or have been rounded then you can't use ==. Instead you must compare against an arbitrary tolerance.
It doesn't change the fact that it can all be done in one line without a loop:
tol = 1e-4; %it's up to you to choose a tolerance value that makes sense for the magnitude of the numbers you want to compare
[X, Y] = find(abs(I - I.' - seachvalue) <= tol)
Ok, now I understand. that's right, I do not need a loop.
[X, Y] = find(abs(I - I.' - seachvalue) <= tol)
With tol is hard to find the value. Is there any way to round the result to the nearest value in the vector?
Thanks
round() can round the number(s) to any number of decimal points that you want.

Sign in to comment.

More Answers (2)

Try this.
for k = 2:length(I)
x = I(k) - I(k-1);
if x == 0.55
disp('Here');
else
disp 'Not here'
end
end

1 Comment

Is not here, but let me ask you what is it doing?
k = 2:length(I)
From the second value to the last.
x = I(k) - I(k-1);
x= the second value - the first one, then the third value - the second one. Is that right?
What happen if the operation is,i.e., the value at the 40 % of the length of the array minus the 20 % of the length of the array? How can I code, try first the sequence you said, then take the 3rd value (k=3:length(I)). When finish, take the third value and substract the first value.
I think it is completely insane for Matlab.

Sign in to comment.

First of all read the FAQ : Click here to learn why you shouldn't use == to compare floating point values. You should use ismembertol():
x = zeros(size(I));
for k = 1 : length(I)
% Take last value and substract the first value of I, second, k times to the last value.
% I have no idea what the above means but let's subtract I(1) and see
x(k) = I(k) - I(1);
if ismembertol(x(k), 0.55, 0.004)
fprintf(' ----> Found a match at index %d where I(%d) = %f and x = %f.\n', k, k, I(k), x);
break; % Let's quit when we find a match.
else
fprintf('Found no match at index %d where I(%d) = %f and x = %f.\n', k, k, I(k), x(k));
end
end
histogram(x);

5 Comments

Hi Image Analyst,
You did not undestand this line
% Take last value and substract the first value of I, second, k times to the last value.
Let me explain it with the code you provided me:
for k = 1 : length(I)
x(k) = I(k) - I(1);
if ismembertol(x(k), 0.55, 0.004)
fprintf(' ----> Found a match at index %d where I(%d) = %f and x = %f.\n', k, k, I(k), x);
break; % Let's quit when we find a match.
else
fprintf('Found no match at index %d where I(%d) = %f and x = %f.\n', k, k, I(k), x(k));
end
end
histogram(x);
Here, the first value is subtracted from the first value. If the condition is not true (ismembertol x ~= 0.55), the second value is subtracted from the first value. If the condition is not true (ismembertol x ~= 0.55), the third value is subtracted from the first value. This rutine is run N times until ismembertol x = 0.55, right?
This is what I tried to explain in this comment. Unfortunaly, I did not explain properly. My fault.
Now, let's back to the code.
In this first round, the value was not found. Therefore, I want to check this operation but using the second value:
x(k) = I(k) - I(2);
The point is that I do not know which value is the one that x=0.55. There are 200 values. Can't change it manually.
I was wondering how the code can go back to x(k) and take I(2) if there was no match with I(1):
n=1
for k = 1 : length(I)
x(k) = I(k) - I(n);
if ismembertol(x(k), 0.55, 0.004)
fprintf(' ----> Found a match at index %d where I(%d) = %f and x = %f.\n', k, k, I(k), x);
break; % Let's quit when we find a match.
else
fprintf('Found no match at index %d where I(%d) = %f and x = %f.\n', k, k, I(k), x(k));
end
%If there is no match, go back to x(k) and take I(2). Do the same operation (I(n+1))
% until x(k) matches 0.55.
Is while statement what I need to use, instead of for loop?
Why use a loop? To subtract all values from I(1) and get a new vector with those differences, do
IDiff = I(1) - I;
Then to see which are closer than 0.004 (or whatever tolerance you want) to 0.55, do
indexes = find(abs(IDiff - 0.55) > 0.004);
I don't see how any loop is needed.
What happen if I want to do this operation n times?
IDiff = I(1) - I;
IDiff = I(2) - I;
IDiff = I(3) - I;
IDiff = I(4) - I;
IDiff = I(5) - I;
IDiff = I(N) - I;
I need a loop for this reason.
I want to continue this operation taking the next value in the case the result is not equal to 0.55.
Do you understand?

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!