필터 지우기
필터 지우기

Add values to vector

조회 수: 4 (최근 30일)
AM
AM 2018년 10월 9일
편집: Stephen23 2018년 10월 9일
I have the following vectors A and B
A= [20 80 -1 0.1 0.05 0.25 0.13 0.12 0.35 21 70 -1 0.1 0.2 0.7 20 77 -1 0.15 0.15 0.7];
B= [1 1 1 1 1 1 1 0 1 1 1 1 0 0 1 1 1 0 0 1 1 1 0 0 0 0 1 1 1 1];
And I would like to create vector C
C= [20 80 -1 0.1 0.05 0.25 0.13 0 0.12 0.35 21 70 0 0 -1 0.1 0.2 0 0 0.7 20 77 0 0 0 0 -1 0.15 0.15 0.7];
I use
I=find(B==0);
to find the indices of B where there is a 0 and I thought about inserting a zero in A in those indices but I do not know how. I also thought about somehow replacing the 1 in B by values of A but again I do not how to do this.
Is there any way to do this?
Thanks in advance

채택된 답변

Stephen23
Stephen23 2018년 10월 9일
편집: Stephen23 2018년 10월 9일
"I also thought about somehow replacing the 1 in B by values of A"
You can use logical indexing. Here is an example that will work if B is either logical or numeric, and does not overwrite the existing variables:
C = double(B);
C(B==1) = A
If B is numeric and you don't mind overwriting its values, then you just need this:
B(B==1) = A
  댓글 수: 1
AM
AM 2018년 10월 9일
Oh I see, thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Adam
Adam 2018년 10월 9일
B( B == 1 ) = A
  댓글 수: 1
AM
AM 2018년 10월 9일
Thank you!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by