Extract the indices based on the minimum absolute value
Ältere Kommentare anzeigen
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
Akzeptierte Antwort
Weitere Antworten (1)
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_B
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!