Filter löschen
Filter löschen

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

4 Ansichten (letzte 30 Tage)
Min
Min am 3 Okt. 2023
Kommentiert: Voss am 3 Okt. 2023
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 Kommentare
Dyuman Joshi
Dyuman Joshi am 3 Okt. 2023
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 am 3 Okt. 2023
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Voss
Voss am 3 Okt. 2023
Bearbeitet: Voss am 3 Okt. 2023
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 Kommentare
Min
Min am 3 Okt. 2023
Awesome! I see the problem now. Thank you!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by