Constructing matrix elements conditionally (and sequentially)

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

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 Loops and Conditional Statements in Help Center and File Exchange

Asked:

D
D
on 24 Oct 2014

Commented:

D
D
on 27 Oct 2014

Community Treasure Hunt

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

Start Hunting!