Adding a number to a vector depending on a different vector

I have a column vector containing only zeros and ones (A) I have another column vector, the same length as A, containing numerous integers (B)
I need to add 128 to the value in B if there is a 1 in A.
so an example of what I need...
A = [ 0 0 0 0 0 1 0 0 0 0 ]';
B = [55 56 54 58 61 60 62 63 61 58]';
In this case, I need to add 128 to the 6th element in B.
How can I automate this please. Thanks

 채택된 답변

Stephen23
Stephen23 2016년 1월 8일
편집: Stephen23 2016년 1월 8일
Use a simple multiplication:
B + A*128
or perhaps logical indexing:
B(A==1) = B(A==1)+128

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

태그

질문:

S
S
2016년 1월 8일

편집:

2016년 1월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by