Generating a matrix with specific sums
Show older comments
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 Comments
Torsten
on 2 Jan 2022
Could you clarify "and so on" ?
The pattern for the summation is not clear to me.
Hylke Dijkstra
on 2 Jan 2022
Image Analyst
on 2 Jan 2022
Isn't that what I did in my Answer below?
Hylke Dijkstra
on 2 Jan 2022
Accepted Answer
More Answers (1)
Another solution:
X = randi([0 400], 6, 6)
kron(eye(3),[1 1])*X
7 Comments
Hylke Dijkstra
on 2 Jan 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
on 2 Jan 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
on 3 Jan 2022
Paul
on 3 Jan 2022
I understood the original question. I wasn't sure what you were asking me to elaborate on in this answer.
Hylke Dijkstra
on 3 Jan 2022
Paul
on 3 Jan 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.
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!