Sum of different column elements of a matrix

5 visualizaciones (últimos 30 días)
Danish Nasir
Danish Nasir el 18 de En. de 2022
Respondida: Max Heimann el 18 de En. de 2022
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.

Respuestas (1)

Max Heimann
Max Heimann el 18 de En. de 2022
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))

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by