필터 지우기
필터 지우기

How can I index the edges of a matrix

조회 수: 4 (최근 30일)
Anthony Womack
Anthony Womack 2019년 1월 30일
댓글: Image Analyst 2019년 1월 30일
My job is the identify only the edges of two matrixes of the same dimension and then take the sum of the product of those two matrixes. How can I avoid including the interna portion of said matrix? Example Matrix [.5, 4, 8, 1.2; 3, 0, 0, 5; 7.4, 0, 0, 3.5; 2, 1, 1.5, 4] where the zeroes are the portion to be ignored.

답변 (1개)

Ollie A
Ollie A 2019년 1월 30일
편집: Ollie A 2019년 1월 30일
I have interpreted your question to mean that for both matrices, you want the set all of the matrix, except the edge, to zero and then multiply the matrices and find the sum. To do this we can use indexing to select only the 'middle' of each matrix:
M1 = ones(n); % Your square matrices
M2 = ones(n);
M1(2:end-1,2:end-1) = 0; % Middle of matrices set to zero
M2(2:end-1,2:end-1) = 0;
sum(sum(M1.*M2));
% The first sum will sum along the first dimension of your product matrix
% giving you a 1xn vector.
I hope this is what you want to achieve!
  댓글 수: 2
Anthony Womack
Anthony Womack 2019년 1월 30일
That was perfect, thank you very much!
Image Analyst
Image Analyst 2019년 1월 30일
Anthony: then go ahead and accept the answer. It's the same code as what I would have suggested. If you don't want to destroy your original matrices, then make a copy of them first, and multiply the copies instead of the originals.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by