Hi, I have X has several numbers inside say: X = [1 4 5 3 2 7 8 4 6 9 3 2 8 5 3 10] I want to first to select the values where X falls below 4, then need to count how many time steps does it take each one to go above 8.
Hope I am clear Thanks

2 Kommentare

Image Analyst
Image Analyst am 8 Jan. 2017
Bearbeitet: Image Analyst am 8 Jan. 2017
So it first goes above 4 at element 3 (where it's 5). Then to go from 5 to 9 (the first time it goes above 8) is 7 steps. Then it goes above 4 at element 14 (where it's 5) then it's 3 steps to get to 10. So is the answer [7, 3]? Or are your rules different than that?
What is the reason why you want this quirky thing?
Karoline Qasem
Karoline Qasem am 9 Jan. 2017
It is slightly different, I am looking at values of dissolved oxygen in water, at some time of the year, it goes BELOW 4 mg/L and it takes couple of days to go to 8 mg/L. My question is how many days does it need to go back from a threshold value (any value less than 4) to reach 8 mg/L
So the only thing not correct in your explanation is that I am looking at values less than 4 not above it. So, looking at the example: the first value below 4 is the first element which is 1, it needs 6 time steps to get to 8.
now moving after that number 4 (the one after 8) it needs two time steps to get to the 9.
etc
Thanks for the comment

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Image Analyst
Image Analyst am 9 Jan. 2017

0 Stimmen

I imagine you've already modified by solution for your updated/clarified question. But anyway, here is the code:
% Define vector.
x = [1 4 5 3 2 7 8 4 6 9 3 2 8 5 3 10]
% Plot it.
plot(x, 'b*-', 'LineWidth', 2, 'MarkerSize', 13)
grid on;
hold on;
line(xlim, [4,4], 'Color', 'r', 'LineWidth', 2);
% Do the scan.
startingIndex = 1;
loopcounter = 1;
while startingIndex < length(x)
subvector = x(startingIndex:end);
% Find where it goes below 4 next. Note ABOVE, not above or equal to.
nextBelow4Index = find(subvector < 4, 1, 'first') + startingIndex - 1;
% Find where it goes above 8 next. Note ABOVE, not above or equal to.
subvector2 = x((nextBelow4Index+1):end);
% Count the number of steps for that to happen.
numSteps(loopcounter) = find(subvector2 >= 8, 1, 'first');
% Move to the next index past the one that exceeded 8.
startingIndex = nextBelow4Index + numSteps(loopcounter) + 1;
loopcounter = loopcounter + 1;
end
numSteps % Report results to command window.
Answer is [6,2,1]

6 Kommentare

Karoline Qasem
Karoline Qasem am 9 Jan. 2017
Thanks for the reply, when I put my original data I got this error Error in SelectRecoveryPeriod (line 54) numSteps(loopcounter) = find(subvector2 >= 8, 1, 'first'); Thanks
Image Analyst
Image Analyst am 9 Jan. 2017
Well you didn't post your original data. You posted this: X = [1 4 5 3 2 7 8 4 6 9 3 2 8 5 3 10] and it works for that. If you can't fix it for your other "original" data, post it, but I'll bet you can figure out what's going wrong since you're a smart engineer.
Karoline Qasem
Karoline Qasem am 9 Jan. 2017
Thanks a lot for the comment, I will try
Karoline Qasem
Karoline Qasem am 9 Jan. 2017
Attached is the sample data, the data am interested in is column 6 the DO. I tried to work it still got the same error, apologize for bothering you but, if you have time to look at I will appreciate. Thanks
Image Analyst
Image Analyst am 9 Jan. 2017
That is a file with lots of columns, many of which have gaps in them. How did you read it in (please give your code)? What column or part of a column was the "X" vector?
Karoline Qasem
Karoline Qasem am 10 Jan. 2017
its the whole column 6, I am attaching it again with it only
thanks

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 8 Jan. 2017
Bearbeitet: Image Analyst am 8 Jan. 2017

0 Stimmen

Try this:
% Define vector.
x = [1 4 5 3 2 7 8 4 6 9 3 2 8 5 3 10]
% Plot it.
plot(x, 'b*-', 'LineWidth', 2, 'MarkerSize', 13)
grid on;
hold on;
line(xlim, [4,4], 'Color', 'r', 'LineWidth', 2);
% Do the scan.
startingIndex = 1;
loopcounter = 1;
while startingIndex < length(x)
subvector = x(startingIndex:end);
% Find where it goes above 4 next. Note ABOVE, not above or equal to.
nextAbove4Index = find(subvector > 4, 1, 'first') + startingIndex - 1;
% Find where it goes above 8 next. Note ABOVE, not above or equal to.
nextAbove8Index = find(subvector > 8, 1, 'first') + startingIndex - 1;
% Count the number of steps for that to happen.
numSteps(loopcounter) = nextAbove8Index - nextAbove4Index;
% Move to the next index past the one that exceeded 8.
startingIndex = nextAbove8Index + 1;
loopcounter = loopcounter + 1;
end
numSteps % Report results to command window.
The result is [7, 3] as expected.

Kategorien

Mehr zu Third-Party Cluster Configuration finden Sie in Hilfe-Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by