Clear Filters
Clear Filters

How do I add to specific parts of an array?

8 views (last 30 days)
Say for example I have A = 5 2 7 4 1 6 3 8 B = 4 7 2 5 8 3 6 1
How would I go about adding B to only values in A that are above 4? so the result is C = 9 2 9 4 1 9 3 9 I know I can use A(A>4) this will give me all values above 5 but I don't know how to add B to those specific values. Just to be clear I don't want to add the lines this is just a simple example the one I am working on is 4000x4000 with the values are more random and have no simple order to them.

Accepted Answer

KSSV
KSSV on 13 Dec 2016
Edited: KSSV on 13 Dec 2016
A = [5 2 7 4 1 6 3 8 ] ;
B = [4 7 2 5 8 3 6 1];
iwant = A ;
iwant(A>4) = A(A>4)+B(A>4)

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!