필터 지우기
필터 지우기

how to check if two arrays intersect within a matrix?

조회 수: 3 (최근 30일)
roshan varkey
roshan varkey 2013년 12월 7일
댓글: Image Analyst 2013년 12월 8일
i have a 10x10 matrix of zeros.
three ships called destroyer,aircraft_carrier and cruiser:
destroyer=[2,2] %placed either vertically or horizontally within the matrix
cruiser=[4,4,4,4]%placed either vertically or horizontally within the matrix
aircraft_carrier=[5,5,5,5,5] %placed either vertically or horizontally within the matrix
i wrote the code so that the ships are placed randomly and also in a random alignment,vertical or horizontal.i want to check weather they intersect with each other so i can write code to prevent it.
i tried using intersect(a,b) command but it gave me back an empty matrix even when the point where intersecting..i dont know what i am doing wrong or if there is some other command to help me do this.....

채택된 답변

Image Analyst
Image Analyst 2013년 12월 7일
편집: Image Analyst 2013년 12월 7일
Try this:
blankElements = (matrix1 == 0) & (matrix2 == 0);
matrixIntersections = (matrix1 ~= matrix2) & ~blankElements;
  댓글 수: 4
Image Analyst
Image Analyst 2013년 12월 8일
roshan's 'Answer' moved here:
Brilliant! thank you very much!
Image Analyst
Image Analyst 2013년 12월 8일
OK, if we're done, then please go ahead and mark it Accepted.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by