Sum of different column elements of a matrix
2 次查看(过去 30 天)
显示 更早的评论
Suppose i have a matrix of size 50x50. I want to sum following elements of thematrix
3rd element of 1st column,8th element of 2nd column,13th elemnt of 3rd column,18th element of 4th column,23rd element of 5th column ,28th elemnt of 6th column,33rd element of 7th column,38th element of 8th column,43rd elemnt of 9th column,48th element of 10th column.
After this sum i want to sum the following
4th element of 11th column,9th element of 12th column,14th element of 13th column,19th element of 14th column,24th element of 15th column ,29th elemnt of 16th column,34rd element of 17th column,39th element of 18th column,44th elemnt of 19th column,49th element of 20th column.
How to code the above problem.
0 个评论
回答(1 个)
Max Heimann
2022-1-18
Matlab supports linear indexing of matrices. Which means you can define a vector with all the matrix elements you want to add like:
linearIndex = [row1 * 50 + column1, row2 * 50 + column2, ...
row1 row2 etc being the elements you want to add. Afterwards you can add them with:
sum(matrix(linearIndex))
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!