Extract the indices based on the minimum absolute value

11 次查看(过去 30 天)
Hi all,
I have two matrices: A and B. Each row of matrix A has an identifier (index). I want to get the identifiers of each row of matrix B based on how much the values in each row are close to each row of matrix A. For example, if the values in the first row of matrix B is the closest to those in the last row of matrix A, assign the identifier of the last row of matrix A to the first row of matrix B, and so on so forth.
My code below tries to do this by calculating the absolute value between each row of B and all the rows of A and chooses the one that returns the minimum absolute value to assign its identifier. However, I think there is something wrong with this as the resulting identifers of matrix B are computed the same as of A.
close
clear all
%define the number of random numbers
n = 10;
%define the min and max ranges of random values
stvar = [0 4]; %on the 1st dim
ndvar = [-1 1]; %on the 2nd dim
%construct the first random matrix
A(:,1) = stvar(1)+(stvar(end)-stvar(1)).*rand(n,1); %first column
A(:,2) = ndvar(1)+(ndvar(end)-ndvar(1)).*rand(n,1); %second column
A = [A(:,1) A(:,2)]; %collect both columns into one matrix
%propose the identifiers of matrix A
iden_A = randi([1,n/2],n,1);
%construct the second random matrix
B(:,1) = stvar(1)+(stvar(end)-stvar(1)).*rand(n,1); %first column
B(:,2) = ndvar(1)+(ndvar(end)-ndvar(1)).*rand(n,1); %second column
B = [B(:,1) B(:,2)]; %collect both columns into one matrix
%finding the identifiers of matrix B
iden_B = [];
%go through all receiving pairs of space and direction
for i = 1:size(B,1)
%define the B pair
B_pair = [B(i,1) B(i,2)];
for j = 1:size(A,1)
abs_value(j,:) = [abs(A(j,1)-B_pair(1)) abs(A(j,2)-B_pair(2))];
end
[~,minloc] = find(min(abs_value(:,1)) & min(abs_value(:,2)));
minloc = iden_A(i);
iden_B = [iden_B ; minloc];
end
Any help would be appreicted.
Thanks

采纳的回答

Abhishek Kumar Singh
编辑:Abhishek Kumar Singh 2024-7-3
Hi @LH
Instead of using the absolute difference for each dimension separately, compute the Euclidean distance between the rows of A and B. This is done using sqrt(sum((A - B_pair).^2, 2)). And then use min(distances) to find the index of the row in A that is closest to the current row in B.
This should ensure that each row in matrix B gets the identifier of the closest row in matrix A.
Refer to the snippet below:
close all
clear all
% Define the number of random numbers
n = 10;
% Define the min and max ranges of random values
stvar = [0 4]; % on the 1st dim
ndvar = [-1 1]; % on the 2nd dim
% Construct the first random matrix
A(:,1) = stvar(1) + (stvar(end) - stvar(1)) * rand(n,1); % first column
A(:,2) = ndvar(1) + (ndvar(end) - ndvar(1)) * rand(n,1); % second column
A = [A(:,1) A(:,2)]; % collect both columns into one matrix
% Propose the identifiers of matrix A
iden_A = randi([1, n/2], n, 1);
% Construct the second random matrix
B(:,1) = stvar(1) + (stvar(end) - stvar(1)) * rand(n,1); % first column
B(:,2) = ndvar(1) + (ndvar(end) - ndvar(1)) * rand(n,1); % second column
B = [B(:,1) B(:,2)]; % collect both columns into one matrix
% Finding the identifiers of matrix B
iden_B = [];
% Go through all receiving pairs of space and direction
for i = 1:size(B,1)
% Define the B pair
B_pair = B(i,:);
% Calculate the Euclidean distance between B_pair and all rows of A
distances = sqrt(sum((A - B_pair).^2, 2));
% Find the index of the minimum distance
[~, minloc] = min(distances);
% Assign the identifier of the closest row in A to the current row in B
iden_B = [iden_B; iden_A(minloc)];
end
% Display the results
disp('Matrix A:');
Matrix A:
disp(A);
3.7020 0.4212 0.6053 -0.3360 1.0224 0.9213 3.7692 0.6286 2.7539 0.9528 3.5434 -0.5355 2.4248 0.8720 0.8152 -0.8384 1.6536 0.9495 3.6147 -0.7808
disp('Identifiers of Matrix A:');
Identifiers of Matrix A:
disp(iden_A);
1 2 5 5 5 3 1 2 5 4
disp('Matrix B:');
Matrix B:
disp(B);
0.9341 0.0032 0.7069 -0.3004 2.5137 -0.5250 1.8370 0.5525 3.6043 -0.0645 2.3333 0.9780 1.9238 -0.2512 3.7867 -0.5267 2.2506 0.4221 3.3141 -0.4006
disp('Identifiers of Matrix B:');
Identifiers of Matrix B:
disp(iden_B);
2 2 3 5 3 1 1 3 1 3
Let's go through the first few rows of B and verify the calculations:
Row 1 of B: ( [0.9341, 0.0032] )
  • Calculate Euclidean distances to each row in A:
distances = sqrt(sum((A - [0.9341, 0.0032]).^2, 2))
= [2.8259, 0.3990, 0.9344, 2.8985, 1.9816, 2.7432, 1.6181, 0.8511, 1.0655, 2.8735]
  • The minimum distance is 0.3990, which corresponds to row 2 in A.
  • Identifier for row 2 in A is 2.
  • So, iden_B(1) = 2.
Row 2 of B: ( [0.7069, -0.3004] )
  • Calculate Euclidean distances to each row in A:
distances = sqrt(sum((A - [0.7069, -0.3004]).^2, 2))
= [3.0764, 0.1033, 1.2538, 3.1488, 2.2337, 2.8363, 1.9807, 0.5451, 1.3885, 2.9975]
  • The minimum distance is 0.1033, which corresponds to row 2 in A.
  • Identifier for row 2 in A is 2.
  • So, iden_B(2) = 2.

更多回答(1 个)

Torsten
Torsten 2024-7-3
编辑:Torsten 2024-7-3
Maybe you mean
%define the number of random numbers
n = 10;
%define the min and max ranges of random values
stvar = [0 4]; %on the 1st dim
ndvar = [-1 1]; %on the 2nd dim
%construct the first random matrix
A(:,1) = stvar(1)+(stvar(end)-stvar(1)).*rand(n,1); %first column
A(:,2) = ndvar(1)+(ndvar(end)-ndvar(1)).*rand(n,1); %second column
A = [A(:,1) A(:,2)]; %collect both columns into one matrix
%propose the identifiers of matrix A
iden_A = randi([1,n/2],n,1);
%construct the second random matrix
B(:,1) = stvar(1)+(stvar(end)-stvar(1)).*rand(n,1); %first column
B(:,2) = ndvar(1)+(ndvar(end)-ndvar(1)).*rand(n,1); %second column
B = [B(:,1) B(:,2)]; %collect both columns into one matrix
%finding the identifiers of matrix B
iden_B = [];
%go through all receiving pairs of space and direction
for i = 1:size(B,1)
%define the B pair
B_pair = [B(i,1) B(i,2)];
for j = 1:size(A,1)
abs_value(j,:) = [abs(A(j,1)-B_pair(1)) abs(A(j,2)-B_pair(2))];
end
[~,minloc] = min(sqrt(abs_value(:,1).^2+abs_value(:,2).^2));
iden_B = [iden_B ; iden_A(minloc)];
end
iden_A
iden_A = 10x1
1 3 4 1 2 4 1 3 3 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
iden_B
iden_B = 10x1
3 3 1 1 1 3 4 1 4 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

类别

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