Subtracting elements within constraints
3 views (last 30 days)
Show older comments
Hi all,
I have a vector and I subtract each element from the previous one using the cumsum function like this
B = bsxfun(@minus, A(1,:), cumsum(A(2:end,:)));
However, the values should be within some particular range (min=1.3 and max=11.7).
So I used this command:
B = max(1.3, min(11.7,(B));
The results before and after the constraint are:
8.896388843 8.896388843
8.896388843 8.896388843
8.896388843 8.896388843
8.896388843 8.896388843
8.896388843 8.896388843
8.896388843 8.896388843
8.896388843 8.896388843
8.896388843 8.896388843
8.896388843 8.896388843
8.896388843 8.896388843
8.896388843 8.896388843
8.896388843 8.896388843
8.896388843 8.896388843
8.896388843 8.896388843
8.896388843 8.896388843
8.896388843 8.896388843
8.896388843 8.896388843
8.899933525 8.899933525
8.899933525 8.899933525
9.315786868 9.315786868
9.743121832 9.743121832
10.41242251 10.41242251
10.8175055 10.8175055
10.83527583 10.83527583
10.84671199 10.84671199
10.92675208 10.92675208
11.31588974 11.31588974
11.7 11.7
11.7 11.7
11.7 11.7
11.74200832 11.7
11.98760913 11.7
12.63688627 11.7
13.29973854 11.7
14.17196338 11.7
14.57395341 11.7
15.06132339 11.7
14.64806384 11.7
14.65909789 11.7
14.67088601 11.7
14.39082286 11.7
13.95882417 11.7
13.52282417 11.7
13.09282417 11.7
12.54482417 11.7
12.00682417 11.7
11.52282417 11.52282417
11.01882417 11.01882417
My problem is that When the value in the vector goes up I dont have a problem as I get the maximum value. However, when the vector start decreasing I want something different:
15.06132339 11.7
14.64806384 11.7 (I want the value 11.7-(15.06132339-14.64806384)= 11.27
Same thing for all the values when decreasing
is there any way to do this?
I don't know if you get my question
thanks anyway!
0 Comments
Answers (2)
Image Analyst
on 28 Feb 2017
Use diff() to find out where the result is negative. When it's negative, the array is decreasing.
differences = diff(B);
decreasingIndexes = differences < 0;
B(decreasingIndexes) = .........
Jan
on 28 Feb 2017
I do not understand: "I subtract each element from the previous one using the cumsum function". Actually diff(A) should do this, or the equivalent A(2:end) - A(1:end-1). But where does the cumsum come from?
It is not clear to me how
15.06132339 11.7
14.64806384 11.7 (I want the value 11.7-(15.06132339-14.64806384)= 11.27
must be generelized for the other elements. Please explain again the procedure to get the results starting from "A" and not from "B".
2 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!