Subtracting elements within constraints

2 visualizaciones (últimos 30 días)
Nikolas Spiliopoulos
Nikolas Spiliopoulos el 28 de Feb. de 2017
Editada: Stephen23 el 28 de Feb. de 2017
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!

Respuestas (2)

Image Analyst
Image Analyst el 28 de Feb. de 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) = .........
  1 comentario
Nikolas Spiliopoulos
Nikolas Spiliopoulos el 28 de Feb. de 2017
thanks for the answer, however I don't really get it how will be the last command. Moreover, I need to do this only when the maximum limit has been reached. I have an Array instead of a vector, so many times the maximum limit is not reached. I still don't get it how to do it thanks!

Iniciar sesión para comentar.


Jan
Jan el 28 de Feb. de 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 comentarios
Nikolas Spiliopoulos
Nikolas Spiliopoulos el 28 de Feb. de 2017
thanks for the answer, actually It subtracts the second element from the first and holds the result. Then the next element is subtracted from the previous result and so on. that's why I am using cumsum.
However, I have an array 48X365, and I do this for each column within min-max range. Diff(A) Gives A diff answer:
if true
example:
A=
1.30000000000000
-0.447611319007917
-0.00239052668507496
0.00222517216126071
0.121515280554814
0.258000000000000
0.148000000000000
0.101000000000000
0.133000000000000
0.260000000000000
0.974000000000000
2.03500000000000
1.44000000000000
0.868000000000000
0.575000000000000
0.459000000000000
0.707000000000000
0.392000000000000
0.382000000000000
end
if true
with cumsum
1.74761131900792
1.75000184569299
1.74777667353173
1.62626139297692
1.36826139297692
1.22026139297692
1.11926139297692
0.986261392976917
0.726261392976917
-0.247738607023083
-2.28273860702308
-3.72273860702308
-4.59073860702308
-5.16573860702308
-5.62473860702308
-6.33173860702308
-6.72373860702308
-7.10573860702308
with Diff(A)
-1.7476
0.4452
0.0046
0.1193
0.1365
-0.1100
-0.0470
0.0320
0.1270
0.7140
1.0610
-0.5950
-0.5720
-0.2930
-0.1160
0.2480
-0.3150
-0.0100
end
Nikolas Spiliopoulos
Nikolas Spiliopoulos el 28 de Feb. de 2017
The elements are positive and negative, so the the value can be decreased or increased!

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by