The problem with the conditions
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Askic V
am 14 Nov. 2022
Verschoben: Jan
am 14 Nov. 2022
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?
Antworten (1)
Vijay
am 17 Nov. 2022
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 Kommentare
Siehe auch
Kategorien
Mehr zu NaNs finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!