Effacer les filtres
Effacer les filtres

How can I extract multiple rows from an array at regular intervals?

1 vue (au cours des 30 derniers jours)
mcb001
mcb001 le 11 Août 2023
Déplacé(e) : Adam Danz le 11 Août 2023
If I have a simple 72x10 array, how can I make a new array that is made up of the first 6 rows, then miss the next 6, then take the next 6 etc. to the end.
If the main array was A, I know B = A(1:6:end,:)would extract me every 6th row for example, but I want to take a 6-row selection of rows. Output B should be 36x10 (i.e. halved in row length)

Réponse acceptée

Adam Danz
Adam Danz le 11 Août 2023
Create demo matrix A
A = (1:72)'.*ones(1,10)
A = 72×10
1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9 10 10 10 10 10 10 10 10 10 10
size(A)
ans = 1×2
72 10
Extract rows 1:6, 13:18, ...
q = 6;
idxMat = reshape(1:q*floor(height(A)/q),q,[]);
idx = idxMat(:,[1:2:end]);
B = A(idx,:)
B = 36×10
1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 13 13 13 13 13 13 13 13 13 13 14 14 14 14 14 14 14 14 14 14 15 15 15 15 15 15 15 15 15 15 16 16 16 16 16 16 16 16 16 16
size(B)
ans = 1×2
36 10

Plus de réponses (1)

Bruno Luong
Bruno Luong le 11 Août 2023
Déplacé(e) : Adam Danz le 11 Août 2023
No need for reshape and repmat, etc...
A = reshape(1:720, 72, 10);
B = A((0:12:66) + (1:6)', :) % 66 is size(A,1)-6
B = 36×10
1 73 145 217 289 361 433 505 577 649 2 74 146 218 290 362 434 506 578 650 3 75 147 219 291 363 435 507 579 651 4 76 148 220 292 364 436 508 580 652 5 77 149 221 293 365 437 509 581 653 6 78 150 222 294 366 438 510 582 654 13 85 157 229 301 373 445 517 589 661 14 86 158 230 302 374 446 518 590 662 15 87 159 231 303 375 447 519 591 663 16 88 160 232 304 376 448 520 592 664

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by