How can I index the edges of a matrix
9 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
Ollie A
on 30 Jan 2019
Edited: Ollie A
on 30 Jan 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 Comments
Image Analyst
on 30 Jan 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.
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!