# Impose a numerical limit on the value of a sum of two vectors

조회 수: 1(최근 30일)
Richard Wood 2021년 7월 16일
댓글: Stephen 2021년 7월 16일
Hello everyone
I want to sum two arrays, let's say a1 and a2. However, I seek that the sum, element by element, of these vectors does not exceed the maximum value of the array a1. Those elements of the sum vector that exceed this limit, I would like them to show the maximum of a1 as a numerical value.
How could I do this?

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

### 채택된 답변

Alan Stevens 2021년 7월 16일
One possibility is
min(max(a1),a1+a2)
##### 댓글 수: 1표시숨기기 없음
Stephen 2021년 7월 16일
+1 this is the efficient MATLAB approach. However it needs to avoid the issues caused by the default behavior of max:
min(max(a1(:)),a1+a2)

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

### 추가 답변(2개)

KSSV 2021년 7월 16일
a12 = a1+a2 ;
[val,idx] = max(a1)
a12(a12>val) = []
##### 댓글 수: 0표시숨기기 이전 댓글 수: -1

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

Jayant Gangwar 2021년 7월 16일
It is my understanding that you would like to sum two arrays element by element and replace all the elements in the resulting array that are greater than the max element in first array by that max element. Given below is an example where I have done the same with two dummy arrays.
a=1:10;
b=1:10;
ans=a+b;
m=max(a);
ans(ans>m)=m

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

### Community Treasure Hunt

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

Start Hunting!

Translated by