Subtracting elements with constraints

조회 수: 7 (최근 30일)
Nikolas Spiliopoulos
Nikolas Spiliopoulos 2017년 2월 27일
댓글: Nikolas Spiliopoulos 2017년 2월 27일
Hi all,
I have an array A and I need to subtract the elements of each column using the cumsum function:
B=bsxfun(@minus, A(1,:), cumsum(A(2:end,:)));
However I have some constraints that should not violated (min=1.3, max=11.7) Is there any way to do the calculations within these limits?
thanks
  댓글 수: 3
Guillaume
Guillaume 2017년 2월 27일
편집: Guillaume 2017년 2월 27일
What does "constraints that should not be violated" actually mean. What should happen to avoid violating these constraints?
Nikolas Spiliopoulos
Nikolas Spiliopoulos 2017년 2월 27일
If the constraints are violated it just put the maximum or minimum value respectively! If it's within the range it does the calculations.

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

채택된 답변

Stephen23
Stephen23 2017년 2월 27일
편집: Stephen23 2017년 2월 27일
Your question is not clear where you want these constraints applied.
Option one: constrain output values:
B = bsxfun(@minus, A(1,:), cumsum(A(2:end,:)));
B = max(1.3, min(11.7, B));
Options two: constrain cumsum values:
tmp = max(1.3, min(11.7, cumsum(A(2:end,:))));
B = bsxfun(@minus, A(1,:), tmp);
  댓글 수: 3
Walter Roberson
Walter Roberson 2017년 2월 27일
Another possibility: that at each point the sum needs to be in that range, and if adding the next element would move it out of the range then clamp it at the edge of the range, but allow the next element to move it away from the edge of the range. For example, if the lower limit was 1, then for
A = 6 2 2 2 2 -3
cummulative
C = 4 2 1 1 4
^ ^
the clamping has been applied at the ^ points, but no history of how far outside the range it would go, so the -(-3) moves it 3 away from the bottom of the range
I am not sure at the moment that a vectorized version of this approach is possible.
Nikolas Spiliopoulos
Nikolas Spiliopoulos 2017년 2월 27일
thanks a lot, I want to constrain the output values!

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by