Generating a matrix with specific sums

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 Commenti

Could you clarify "and so on" ?
The pattern for the summation is not clear to me.
Sure, in this case I would like to sum the first and second row, the fourth and fifth row, and the fifth and sixth row for each column, that's why my matrix is a 3x6 matrix in the end. Preferably, I would like to do the same for summing the first to third row and so on, but I am sure I can manage that starting with the former.
This gives me:
X =
161 167 135 96 230 17
30 19 360 161 23 67
96 361 148 38 94 260
49 378 44 52 141 293
73 196 312 377 329 259
96 196 156 383 6 180
Now I want to do generate the following matrix in a succinct manner:
Y =
191 x12+x22 .. .. .. ..
145 x32+x42 .. .. .. ..
163 x52+x62 .. .. .. ..
The .. obviously follow the same logic
Isn't that what I did in my Answer below?
Yes, it is! I wrote this reply before I noticed you helped out, thanks!

Accedi per commentare.

 Risposta accettata

The pattern is confusing, like Torsten says. Is this what you mean?
X = randi([0 400], 6, 6)
X = 6×6
395 191 150 346 373 27 235 335 369 34 49 224 158 234 266 205 264 328 382 31 302 279 44 131 86 393 156 130 331 188 260 137 229 312 185 345
X2 = [...
sum(X(1:2, :), 1); ...
sum(X(3:4, :), 1); ...
sum(X(5:6, :), 1); ]
X2 = 3×6
630 526 519 380 422 251 540 265 568 484 308 459 346 530 385 442 516 533

4 Commenti

Yes, it is exactly what I mean! Thanks, would you have any advice on what to read to reproduce this for other examples?
For a more general solution with variable numbers of rows, you can use blockproc() so read the documentation for that and run my attached demos. blockproc() requires the Image Processing Toolbox.
Thank you!
If you have some other even number of rows, you can use a for loop:
X = randi([0 400], 8, 3)
X = 8×3
350 342 147 234 99 127 16 366 80 136 333 267 227 281 388 127 313 179 313 213 244 207 62 4
[rows, columns] = size(X);
X2 = zeros(rows/2, columns);
newRow = 1;
for row = 1 : 2 : rows
X2(newRow, :) = sum(X(row : row+1, :), 1);
newRow = newRow + 1; % Increment pointer
end
X2 % Show in command window.
X2 = 4×3
584 441 274 152 699 347 354 594 567 520 275 248

Accedi per commentare.

Più risposte (1)

Another solution:
X = randi([0 400], 6, 6)
X = 6×6
201 154 330 38 350 3 65 77 370 157 11 316 97 86 189 251 231 29 233 238 74 4 248 64 374 336 400 168 244 26 377 339 53 90 291 364
kron(eye(3),[1 1])*X
ans = 3×6
266 231 700 195 361 319 330 324 263 255 479 93 751 675 453 258 535 390

7 Commenti

Would you care to elaborate? Much appreciated.
I'm not sure what you're asking for. Left multiplication with this matrix
kron(eye(3),[1 1])
ans = 3×6
1 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1
generates the desired result by a matrix multiplicaton.
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.
Exactly, thanks!
I understood the original question. I wasn't sure what you were asking me to elaborate on in this answer.
Yes, thanks once again. I would like to be able to replicate what you did but also get the intuition behind it.
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.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by