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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by