How to extract rows given multiple ranges

11 vues (au cours des 30 derniers jours)
Jacob Ebilane
Jacob Ebilane le 30 Mai 2022
Modifié(e) : KSSV le 30 Mai 2022
So i have a dataset and I'm trying to downsize it, I have data sorted and have extracted the row indeces required to downsize the dataset while maintaining an almost equal amount of samples per class.
Here are the the specified rows. The dataset has 500,000 samples, using these rows downsizes it to about 120,000. I just want to know what I can to extract these from the original dataset keeping all the columns.
[0:5000,34586:39586,72961:77961,107165:112165,142309:147309,175845:180845,207262:212262,241495:246495,277250:282250,311197:316197,345045:350045,351453:354453,355332:359332,359895:362895,363048:365048,365517:370517,373755:378755,398739:403739,408560:413560,421163:425163,425859:427859,428631:433631,438809:443809,447548:452548,462867:467867,474286:476286,477036:479036,480031:485031,498294:500294]

Réponse acceptée

KSSV
KSSV le 30 Mai 2022
Modifié(e) : KSSV le 30 Mai 2022
If you have the indices of rows idx, you can just use
iwant = A(idx,:) ; % where A is your matrix and idx are the row indices
to extract the rows from A wih all the columns.
Note: MATLAB indexing start from 1 and indices should be positive integers or logicals.
In your case, the first number: 0:5000 should be 1:5000.
% EXample
A = magic(10) % dummy data for demo
A = 10×10
92 99 1 8 15 67 74 51 58 40 98 80 7 14 16 73 55 57 64 41 4 81 88 20 22 54 56 63 70 47 85 87 19 21 3 60 62 69 71 28 86 93 25 2 9 61 68 75 52 34 17 24 76 83 90 42 49 26 33 65 23 5 82 89 91 48 30 32 39 66 79 6 13 95 97 29 31 38 45 72 10 12 94 96 78 35 37 44 46 53 11 18 100 77 84 36 43 50 27 59
idx = [1 3 5 7] ; % extrcat these rows from A
iwant = A(idx,:)
iwant = 4×10
92 99 1 8 15 67 74 51 58 40 4 81 88 20 22 54 56 63 70 47 86 93 25 2 9 61 68 75 52 34 23 5 82 89 91 48 30 32 39 66

Plus de réponses (0)

Catégories

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

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by