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

조회 수: 3 (최근 30일)
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

채택된 답변

Guillaume
Guillaume 2020년 2월 1일
편집: Guillaume 2020년 2월 1일
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
Jose Rego Terol
Jose Rego Terol 2020년 2월 2일
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
Image Analyst
Image Analyst 2020년 2월 2일
round() can round the number(s) to any number of decimal points that you want.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Subhadeep Koley
Subhadeep Koley 2020년 2월 1일
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
Jose Rego Terol
Jose Rego Terol 2020년 2월 1일
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.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2020년 2월 1일
편집: Image Analyst 2020년 2월 1일
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
Jose Rego Terol
Jose Rego Terol 2020년 2월 1일
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.
Jose Rego Terol
Jose Rego Terol 2020년 2월 1일
I want to continue this operation taking the next value in the case the result is not equal to 0.55.
Do you understand?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by