The problem with the conditions
조회 수: 1 (최근 30일)
이전 댓글 표시
I have a problem with the conditions by which the code does not work properly. I would like the code to subtract a vector t1 from t only when it is in a specific range. When it is not in a particular range it is supposed to add more vectors until it is in a particular range.
I have a code like the following:
indexA = 1; % posiotion in vector a
indexB =1; % positon in vector b
for indexB = 1:length(t1)
c=t1(indexB)-t(indexA);
if (c<0.2600) % condition
disp(t(indexA)) %
disp(t1(indexB))
d(indexB)=c;
%wynik_pat=abs(d);
disp('----------')
else
indexA=indexA+1;
c=t1(indexB)-t(indexA);
while (c>0.2600)
indexA=indexA+1;
c=t1(indexB)-t(indexA);
d(indexB)=c;
end
disp(t(indexA)) %
disp(t1(indexB))
d(indexB)=c;
disp('----------')
end
end
plot(d)
댓글 수: 2
Askic V
2022년 11월 14일
이동: Jan
2022년 11월 14일
In order to get a good response, it would be probably the best if you could provide simple examples of your vectors t1 nad t and how output should look like. That way it would be much higher chance to get an answer.
What does it mean to "add more vectors", when in your code you just iterate through elements of a vector?
답변 (1개)
Vijay
2022년 11월 17일
Hello @<person>
Is it guaranteed that after sampling from t and subtracting we will have a value in the desired range before we run out of vector t?
If not, then you need to have bound checks in while loop and add code to handle above scenario.
Example:
T1 = [1, 2, 3, 4];
T = [0, 0, 0, 0];
Consider another case
T1 = [1, 2, 3, 4];
T = [0.001, 0.002, 0.003, 0.996];
The ‘1’ in T1 consumes the entire array T, now the remaining values in the array cannot be reduced to <= 0.26. You will have to handle this case.
Adding comments in code to highlight the issues.
indexA = 1; % positionin vector a
indexB =1; % position in vector b
for indexB = 1:length(t1)
c=t1(indexB)-t(indexA);
if (c<0.2600) % condition
disp(t(indexA)) %
disp(t1(indexB))
d(indexB)=c;
%wynik_pat=abs(d);
disp('----------')
else
indexA=indexA+1;
c=t1(indexB)-t(indexA);
while (c>0.2600) % you will run out of array t.
indexA=indexA+1; %%what if the first value in T1 consumes all indices of A.
c=t1(indexB)-t(indexA);
d(indexB)=c;
end
disp(t(indexA)) %
disp(t1(indexB))
d(indexB)=c;
disp('----------')
end
end
plot(d)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 NaNs에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!