MATLAB Answers

## How to apply a condition on array?

Asked by Hajem Daham

### Hajem Daham (view profile)

on 8 Dec 2018
Latest activity Commented on by Hajem Daham

### Hajem Daham (view profile)

on 9 Dec 2018
a = [1 2 3 4 5]
x(a) = [15 20 45 25 30]
b = [6 7 8 9 10]
x(b) = [20 25 30 15 15]
t=40
the below array ab is a random permutation of a and b elemnts and then zeros are inserted.
ab = 5 2 6 0 4 9 8 7 0 1 10 0 3
I want to apply this condition on the between zeros of array ab:
the condition is that if the values of the corresponding values of x(a) and x(b) for a and b elemnts between zeros are greater than t, in this case add a penalty of 100. Then acummulate the total penalty of these between zeros values
This condition includ the mix case of 1-5 and 6-10 between zeros and the individaul case like 3 at the end of ab.

#### 4 Comments

Show 1 older comment
Image Analyst

### Image Analyst (view profile)

on 9 Dec 2018
There is a zero at ab index 12, yet x doesn't have an index 12 - it's only 10 long - so what is your plan there?
Hajem Daham

### Hajem Daham (view profile)

on 9 Dec 2018
Sorry for confusing, let me explain in more details.
take the first between zeros elements 5 2 6 the x(a)'s values for 5 is 30 and for 2 is 20, the sum of x(a) for 5 and 2 is 50 which is largere than 40, so in this case there is a 100 penalty. The x(b) for 6 is 20 which is less than 40 so the penalty is 0, the total penalty for the 5 2 6 is 100. Similarly take the second between zeros elements which are 4 9 8 7, the x(a) value for element 4 is 25 which is less than 40 so the penalty is 0, while the x(b)'s values for 9 8 7 are 15 30 25= 70 which is large than 40 so the penalty for elemnts 9 8 7 is 100 and the total penalty for the 4 9 8 7 is 100. Similarly for other between zeros. Finaly I want the total sum of penalty for all all between zeros elemnts. A loop should be made for this process.
Hope it is clear now.
Hajem Daham

### Hajem Daham (view profile)

on 9 Dec 2018
Image Analyst, thereis no x(a) or x(b) for the zeros in ab, so please ignore the zeros in ab just focus on values between zeros.

Sign in to comment.