Borrar filtros
Borrar filtros

How can I index the edges of a matrix

9 visualizaciones (últimos 30 días)
Anthony Womack
Anthony Womack el 30 de En. de 2019
Comentada: Image Analyst el 30 de En. de 2019
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.

Respuestas (1)

Ollie A
Ollie A el 30 de En. de 2019
Editada: Ollie A el 30 de En. de 2019
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 comentarios
Anthony Womack
Anthony Womack el 30 de En. de 2019
That was perfect, thank you very much!
Image Analyst
Image Analyst el 30 de En. de 2019
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by