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
Post a Comment