matlab - Adding a quantity to elements of a matrix that are higher than a quantity -


how can add 8 elements of matrix

a=[1 7 5 7 2 3 8 2 9 5] 

that greater 3 without using for-loop? desired result matrix

b=[1 15 13 15 2 3 16 2 17 13] 

you can create logical vector, each of elements of a larger 3 1, , not larger 3 0. vector can multiplied 8, , added original a vector:

b = + 8 * (a>3); 

breakdown

create logical vector:

a>3 ans =    0   1   1   1   0   0   1   0   1   1 

multiply vector 8:

8 * (a>3) ans =    0   8   8   8   0   0   8   0   8   8 

and add a:

b = + 8*(a>3) b =     1   15   13   15    2    3   16    2   17   13 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -