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

 채택된 답변

Roger Stafford
Roger Stafford 2014년 10월 24일
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.)

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

D
D
2014년 10월 24일

댓글:

D
D
2014년 10월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by