How do I return the indexed values unsorted, in the original order in which they appear in a vector?

1 vue (au cours des 30 derniers jours)
What I'm trying to accomplish seems like it should be much easier, however I acknowledge that I am not proficient at all and am probably misunderstanding the utlity of this indexing.
There's a couple things I am trying to see if I can resolve:
1) radius_gl ( as shown below is 18 x3) which is not allowing the return of indexed values beyond the limit of 18 even though gamt/lamt have repeat values.
a) Is there a way around this?
2) r1 and r2 was previously returning only 12 or 11 indexed values; this was not the right because 20 values should have been returned, and they were in a sorted order
This is the current code:
75 15 25 % col_1 = G; col_2 = L; col_3 = radii;
77 17 25
79 19 50
81 21 50
83 23 75
85 25 75
87 27 100
89 29 100
91 31 125
93 33 125
95 35 150
97 37 150
99 39 175
101 41 175
103 43 200
105 45 200
107 47 225
109 49 225
% heres a snippet of the lamt (20 x 1); gamt is structured the same but has
% different range of numbers
47
43
21
43
17
17
47
45
43
19
33
29
idx_gamt = find(ismember(gamt, radius_gl(:, 1),"rows"));
idx_lamt = find(ismember(lamt, radius_gl(:, 2), "rows"));
r1 = radius_gl(idx_gamt, 3);
r2 = radius_gl(idx_lamt, 3);

Réponse acceptée

Voss
Voss le 7 Jan 2024
Modifié(e) : Voss le 7 Jan 2024
radius_gl = [75 15 25 % col_1 = G; col_2 = L; col_3 = radii;
77 17 25
79 19 50
81 21 50
83 23 75
85 25 75
87 27 100
89 29 100
91 31 125
93 33 125
95 35 150
97 37 150
99 39 175
101 41 175
103 43 200
105 45 200
107 47 225
109 49 225];
lamt = [47
43
21
43
17
17
47
45
43
19
33
29];
[ism_lamt,idx_lamt] = ismember(lamt,radius_gl(:,2));
r2 = radius_gl(idx_lamt(ism_lamt),3)
r2 = 12×1
225 200 50 200 25 25 225 200 200 50
Or, if you know all elements of lamt are in radius_gl(:,2), you can do away with ism_lamt:
[~,idx_lamt] = ismember(lamt,radius_gl(:,2));
r2 = radius_gl(idx_lamt,3)
r2 = 12×1
225 200 50 200 25 25 225 200 200 50
  2 commentaires
Jade T
Jade T le 7 Jan 2024
Yes, I know all of the elements of lamt are in radius_gl. I wanted to be able to store them in the order they appear. Thank you very much!

Connectez-vous pour commenter.

Plus de réponses (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov le 7 Jan 2024
Understood your question correctly, this is what you are trying to achieve:
radius_gl = readmatrix('D_GAMT.txt'); % Copied your sample data
lamt = readmatrix('D_LAMT.txt'); % Copied your sample data
gamt = lamt+randi(2, numel(lamt), 1); % Sample data created
idx_gamt = find(ismember(radius_gl(:, 1), gamt, "rows"));
idx_lamt = find(ismember(radius_gl(:, 2), lamt, "rows"));
r1 = radius_gl(idx_gamt, 3)
r1 = 0×1 empty double column vector
r2 = radius_gl(idx_lamt, 3)
r2 = 8×1
25 50 50 100 125 200 200 225
  1 commentaire
Jade T
Jade T le 7 Jan 2024
This is also what I got, but if you see in lamt, the first number in the column vector is 47 so r2 should shart with 225 and so on; in short, I want them ordered as they are in lamt, not numerically ordered consecutively. Does that make sense?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Tags

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by