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

1 次查看(过去 30 天)
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);

采纳的回答

Voss
Voss 2024-1-7
编辑:Voss 2024-1-7
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

更多回答(1 个)

Sulaymon Eshkabilov
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 个评论
Jade T
Jade T 2024-1-7
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?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

标签

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by