Constructing matrix elements conditionally (and sequentially)

1 view (last 30 days)
Hi,
I have two matrices of differing lengths:
A = [1 1 0 1 0 0 1 0 1 0 1] B = [25.4 17.3 11.6 9.2 14.3 15.0]
I want to construct a matrix (C) that is the same length of A, but the 1 values are sequentially replaced by those in matrix B to yield:
C = [25.4 17.3 0 11.6 0 0 9.2 0 14.3 0 15.0]
Can anyone direct me to any relevant functions/methods that can do this?
As an aside, sum(A)==length(B) in all cases. However, sum(A) and length(B) will both vary with each iteration.
Thank you immensely.
Dave

Accepted Answer

Roger Stafford
Roger Stafford on 24 Oct 2014
C = zeros(size(A));
C(A>0) = B;
(Note that this will give an error if your assumption that sum(A)==length(B) is not true.)

More Answers (0)

Categories

Find more on Get Started with MATLAB 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!