How can I index the edges of a matrix
6 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposte (1)
Ollie A
il 30 Gen 2019
Modificato: Ollie A
il 30 Gen 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 Commenti
Image Analyst
il 30 Gen 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.
Vedere anche
Categorie
Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!