How can I index the edges of a matrix
10 次查看(过去 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.
0 个评论
回答(1 个)
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 个评论
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 Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!