필터 지우기
필터 지우기

Setting an element of an array to zero based on the values of a vector

조회 수: 2 (최근 30일)
L'O.G.
L'O.G. 2022년 4월 20일
편집: Matt J 2022년 4월 20일
Given a 2D array with elements a(i,j), if either i or j matches any element in another vector, how do you set the value for that a(i,j) = 0?
So, for example, given A = magic(5), how do you set to 0 any element of A given a vector B = [1 2 5 8] if 1, 2, 5, or 8 is an entry in A?
Here, I want to obtain:
A =
17 24 0 0 15
23 0 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 0 9

채택된 답변

Matt J
Matt J 2022년 4월 20일
  댓글 수: 4
L'O.G.
L'O.G. 2022년 4월 20일
I suppose I didn't put it well. I meant in this case, A(1,1) = 17, but 17 is not in B and therefore A(1,1) should be left as it is.
Matt J
Matt J 2022년 4월 20일
편집: Matt J 2022년 4월 20일
A = magic(5),
A = 5×5
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
B = [1 2 5 8];
A( ismember(A,B) )=0
A = 5×5
17 24 0 0 15 23 0 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 0 9

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by