How to sum specific elements in a row of a matrix
5 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Ameer Hamza
2020-3-27
Try this:
m = rand(256,256);
mask = reshape((1:16:256)+(0:3)', 1, []);
result = sum(m(:,mask), 2);
另请参阅
类别
在 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!