Generating a matrix with specific sums
Mostrar comentarios más antiguos
Dear all,
I am trying to generate a matrix based on specific sums. Let's say I generate the following matrix
X = randi([0 400], 6, 6);
Which gives me the matrix X. Now I want to sum x11 and x21, x12 and x22, x21 and x22, and so on. This should give me a 3x6 matrix. Does anyone have any clue how to do this effectively?
Best
4 comentarios
Torsten
el 2 de En. de 2022
Could you clarify "and so on" ?
The pattern for the summation is not clear to me.
Hylke Dijkstra
el 2 de En. de 2022
Image Analyst
el 2 de En. de 2022
Isn't that what I did in my Answer below?
Hylke Dijkstra
el 2 de En. de 2022
Respuesta aceptada
Más respuestas (1)
Another solution:
X = randi([0 400], 6, 6)
kron(eye(3),[1 1])*X
7 comentarios
Hylke Dijkstra
el 2 de En. de 2022
I'm not sure what you're asking for. Left multiplication with this matrix
kron(eye(3),[1 1])
generates the desired result by a matrix multiplicaton.
Image Analyst
el 2 de En. de 2022
I think he just wanted to block process it so that each pair of rows goes into one row of the output image. Like
new row 1 = row 1 + row 2
new row 2 = row 3 + row 4
new row 3 = row 5 + row 6
new row 4 = row 7 + row 8
and so on.
Hylke Dijkstra
el 3 de En. de 2022
Paul
el 3 de En. de 2022
I understood the original question. I wasn't sure what you were asking me to elaborate on in this answer.
Hylke Dijkstra
el 3 de En. de 2022
Paul
el 3 de En. de 2022
If you want to know more about the kron() function, start with its doc page.
Mathematically, all that's happening is matrix multiplication. Is that the part of the solution you're asking about?
FWIW, this solution probably isn't the most efficient what with all the multiplications by zero and summing up terms that are zero. Probably better to just pre-allocate the answer and use a simple loop over the sum() function (basically a minor extension of Image Analyst's solution) to fill in the rows of the anwer.
Categorías
Más información sobre Matrix Indexing en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!