extract a row of 2D array based on constant 2D array values

4 次查看(过去 30 天)
hi I am trying to pull out / extract number of rows from a 2D array data based off from another 2D array (1 row).
For example,
A = [90 10 21; 90 20 21; 90 30 21; 90 40 22; 90 50 21; 88 10 20; 88 20 20; 88 30 24;
88 40 22; 88 50 21; 86 10 20; 86 20 21; 86 30 20; 86 40 25; 86 50 20]
A = 15×3
90 10 21 90 20 21 90 30 21 90 40 22 90 50 21 88 10 20 88 20 20 88 30 24 88 40 22 88 50 21
B = [14 26 30 47]
B = 1×4
14 26 30 47
result = [90 10 21; 90 30 21; 90 30 21; 90 50 21; 88 10 20; 88 30 24;
88 30 24; 88 50 21; 86 10 20; 86 30 20; 86 30 20; 86 50 20]
result = 12×3
90 10 21 90 30 21 90 30 21 90 50 21 88 10 20 88 30 24 88 30 24 88 50 21 86 10 20 86 30 20
I want to pull the entire row of A if the 2nd column of A has the closest value from B. But the second column of A is repeating 10 - 50.
I tried the interp1 (A,A,B,'nearest') but it does not work since B must be a target value.
Can you suggest any other solutions?
  4 个评论
Dyuman Joshi
Dyuman Joshi 2023-10-3
The criteria/logic to get the output result from the inputs A and B is still not clear to me.
Could you elaborate on it?
Min
Min 2023-10-3
Sure!
Well a quick update is that it does not need to be nearest anymore. I just found a way to get those values.
I was wondering if there is a way to extract a number of rows based on the 'B' array values.
So
let's say
A Column name: Column 1 = Distance, Column 2 = Angle, Column 3 = something else
where A = [90, 10, 21; 90, 20, 21; 90, 30, 21; 90, 40, 22; 90, 50, 21; 88, 10, 20; 88, 20, 20; 88, 30, 24; 88, 40, 22; 88, 50, 21; 86, 10, 20; 86, 20, 21; 86, 30, 20; 86, 40, 25; 86, 50, 20]
Then based on the Array 'B' where B = [14; 26; 30; 47]
Then it will select and extract the a number of rows from A when column 2 of A has the nearest B values.
Essentially, extracting the rows when it matches another array.

请先登录,再进行评论。

采纳的回答

Voss
Voss 2023-10-3
编辑:Voss 2023-10-3
A = [90 10 21; 90 20 21; 90 30 21; 90 40 22; 90 50 21; 88 10 20; 88 20 20; 88 30 24; 88 40 22; 88 50 21; 86 10 20; 86 20 21; 86 30 20; 86 40 25; 86 50 20]
A = 15×3
90 10 21 90 20 21 90 30 21 90 40 22 90 50 21 88 10 20 88 20 20 88 30 24 88 40 22 88 50 21
B = [14; 26; 30; 47]
B = 4×1
14 26 30 47
[~,~,jj] = unique(A(:,1),'stable');
result = splitapply(@(x)get_rows_near(x,B(:).'),A,jj);
result = vertcat(result{:});
disp(result);
90 10 21 90 30 21 90 30 21 90 50 21 88 10 20 88 30 24 88 30 24 88 50 21 86 10 20 86 30 20 86 30 20 86 50 20
function out = get_rows_near(x,B)
[~,idx] = min(abs(x(:,2)-B),[],1);
out = {x(idx,:)};
end
  6 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by