Replacing 1's in a vector 'a' with elements of another vector 'b' where a and b are not equal?

1 view (last 30 days)
I have two vectors 'a' and 'b' in the following form:
a=[nan nan 1 1 nan 1 nan 1 1 1 1]
b=[2 3 4 5 6 7 8]
I want to replace all the 1's in vector 'a' by elements in vector 'b' so that I have a vector 'c' in the following shape:
c=[nan nan 2 3 nan 4 nan 5 6 7 8]
I have vectors 'a' and 'b' having thousands of elements and I want to replace all 1's in vector 'a' by elements in vector 'b' .
Please help
Thanks

Accepted Answer

Stephen23
Stephen23 on 22 Jul 2015
Edited: Stephen23 on 22 Jul 2015
>> a = [NaN,NaN,1,1,NaN,1,NaN,1,1,1,1];
>> b = [2,3,4,5,6,7,8];
>> a(a==1) = b
a =
NaN NaN 2 3 NaN 4 NaN 5 6 7 8

More Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 22 Jul 2015
a=[nan nan 1 1 nan 1 nan 1 1 1 1]
b=[2 3 4 5 6 7 8]
idx=a==1
a(idx)=b(1:sum(idx))

Community Treasure Hunt

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

Start Hunting!