The problem with the conditions

조회 수: 1 (최근 30일)
Krystian Sliwka
Krystian Sliwka 2022년 11월 14일
답변: Vijay 2022년 11월 17일
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
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?
Krystian Sliwka
Krystian Sliwka 2022년 11월 14일
sample data:
t1 = [0.196000000000000 1.13900000000000 2.03600000000000 3.04600000000000 4.04900000000000 4.93400000000000 5.90100000000000 6.97500000000000 7.95300000000000 8.83400000000000 9.61600000000000 10.3740000000000 11.1410000000000 12.0020000000000 12.8700000000000 14.0140000000000 15.1210000000000 16.1940000000000 17.2230000000000 18.1150000000000 18.8850000000000 19.6220000000000 20.4060000000000 21.2920000000000 22.2280000000000 23.1800000000000 24.1070000000000 24.9870000000000 25.8710000000000 26.7790000000000 27.6820000000000 28.6010000000000 29.5120000000000 30.3530000000000 31.1680000000000 32.0640000000000 33.0270000000000 33.9180000000000 34.7930000000000 35.7840000000000 36.8060000000000 37.7130000000000 38.6610000000000 39.6720000000000 40.5790000000000 41.4500000000000 42.4420000000000 43.4790000000000 44.4190000000000 45.3440000000000 46.3700000000000 47.4230000000000 48.3910000000000 49.3320000000000 50.4140000000000 51.5180000000000 52.5440000000000 53.4820000000000 54.4680000000000 55.5970000000000 56.7320000000000 57.7540000000000 58.7790000000000 59.8600000000000 60.8690000000000 61.8090000000000 62.9540000000000 64.0280000000000 65.0040000000000 66.1040000000000 67.2660000000000 68.3030000000000 NaN 87 88.0120000000000 88.9140000000000 89.9530000000000 91.0270000000000 92.0150000000000 93.0150000000000 94.0890000000000 95.1110000000000 96.0140000000000 97.0600000000000 98.1070000000000 99.0940000000000 100.186000000000 101.271000000000 102.237000000000 103.244000000000 104.304000000000 105.310000000000 106.276000000000 107.339000000000 108.378000000000 109.332000000000 110.356000000000 111.413000000000 112.409000000000 113.409000000000 114.486000000000 115.481000000000 116.406000000000 117.437000000000 ]
t = [ 0.0840000000000000 0.935000000000000 1.83900000000000 2.85400000000000 3.84500000000000 4.73200000000000 5.71000000000000 6.77900000000000 7.75000000000000 8.62400000000000 9.40900000000000 10.1720000000000 10.9430000000000 11.8060000000000 12.6770000000000 13.8250000000000 14.9320000000000 16.0030000000000 17.0310000000000 17.9190000000000 18.6890000000000 19.4270000000000 20.2120000000000 21.1040000000000 22.0370000000000 22.9860000000000 23.9090000000000 24.7900000000000 25.6760000000000 26.5840000000000 27.4870000000000 28.4060000000000 29.3130000000000 30.1520000000000 30.9750000000000 31.8760000000000 32.8330000000000 33.7180000000000 34.6020000000000 35.5970000000000 36.6070000000000 37.5170000000000 38.4710000000000 39.4750000000000 40.3770000000000 41.2570000000000 42.2520000000000 43.2800000000000 44.2160000000000 45.1490000000000 46.1790000000000 47.2260000000000 48.1870000000000 49.1360000000000 50.2220000000000 51.3240000000000 52.3400000000000 53.2800000000000 54.2750000000000 55.4050000000000 56.5300000000000 57.5530000000000 58.5850000000000 59.6640000000000 60.6640000000000 61.6140000000000 62.7610000000000 63.8230000000000 64.8040000000000 65.9080000000000 67.0560000000000 68.0810000000000 69.1380000000000 70.2060000000000 71.1900000000000 72.2370000000000 73.3250000000000 74.3050000000000 75.3310000000000 76.4380000000000 77.4460000000000 78.4250000000000 79.4900000000000 80.5880000000000 81.5760000000000 82.6540000000000 83.7080000000000 84.6390000000000 85.6360000000000 86.7050000000000 87.7210000000000 88.6310000000000 89.6890000000000 90.7540000000000 91.7460000000000 92.7570000000000 93.8380000000000 94.8490000000000 95.7690000000000 96.8210000000000 97.8640000000000 98.8580000000000 99.9570000000000 101.031000000000 102.005000000000 103.017000000000 104.083000000000 105.079000000000 106.070000000000 107.140000000000 108.169000000000 109.131000000000 110.160000000000 111.211000000000 112.207000000000 113.216000000000 114.290000000000 115.276000000000 116.210000000000 117.243000000000 ]
subtracting t1 from t I expect results in the range of 0.2600 to 0.1900
Looking at t1 we can see that we have a value of 68 and then a value of NaN and then 87. I would like the program to take successive samples from the t vector until, after subtracting t1-t, I get a value between 0.2600 and 0.1900

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

답변 (1개)

Vijay
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)

카테고리

Help CenterFile Exchange에서 NaNs에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by