Finding a value of one vector based on the nonzero values of another vector
조회 수: 3 (최근 30일)
이전 댓글 표시
Given two vectors, how do you determine the value of A where B is 1? In this case, A=[5 4]. How do I do this?
A = [2 5 1 4];
B = [0 1 0 1];
댓글 수: 0
답변 (3개)
Image Analyst
2022년 12월 8일
If B is always guaranteed to be 0 or 1, you can do this
A = [2 5 1 4];
B = [0 1 0 1]; % B is a double here.
A = A(logical(B)) % Need to cast B to logical
If B can be anything and you want to get A where B is not equal to zero:
A = [2 5 1 4];
B = [0 23 0 341];
A = A(B ~= 0)
댓글 수: 1
Stephen23
2022년 12월 8일
Using LOGICAL() works in both cases:
A = [2 5 1 4];
B = [0 23 0 341];
A = A(logical(B))
참고 항목
카테고리
Help Center 및 File Exchange에서 Author Block Masks에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!