Effacer les filtres
Effacer les filtres

How to sum specific elements in a row of a matrix

6 vues (au cours des 30 derniers jours)
Ashton Linney
Ashton Linney le 27 Mar 2020
Commenté : Ashton Linney le 27 Mar 2020
I have a 256 by 256 matrix, lets say it is called m.
I would like to sum specific elements from a specific row in that matrix.
For example, just looking at row 1, I would like to sum the following of these elements:
1-4, (multiples of 16) + 1, (multiples of 16) + 2, (multiples of 16) + 3, and (multiples of 16) + 4.
Therefore I will end up with a single value for each row of the matrix.
Please can somebody help me with the coding for this?
Thank you

Réponse acceptée

Ameer Hamza
Ameer Hamza le 27 Mar 2020
Try this:
m = rand(256,256);
mask = reshape((1:16:256)+(0:3)', 1, []);
result = sum(m(:,mask), 2);
  1 commentaire
Ashton Linney
Ashton Linney le 27 Mar 2020
That is absolutely perfect! Thank you Ameer!

Connectez-vous pour commenter.

Plus de réponses (1)

darova
darova le 27 Mar 2020
Modifié(e) : darova le 27 Mar 2020
try this
A = rand(256);
srow = 0;
for i = 1:16:size(A,1)
srow = srow + sum(A(i:i+3,:),1);
end

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by