フィルターのクリア

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

4 ビュー (過去 30 日間)
Min
Min 2023 年 10 月 3 日
コメント済み: Voss 2023 年 10 月 3 日
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 件のコメント
Min
Min 2023 年 10 月 3 日
Awesome! I see the problem now. Thank you!
Voss
Voss 2023 年 10 月 3 日
You're welcome!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by