## Extract the indices based on the minimum absolute value

Hi all,

I have two matrices: and . Each row of matrix has an identifier (index). I want to get the identifiers of each row of matrix based on how much the values in each row are close to each row of matrix . For example, if the values in the first row of matrix is the closest to those in the last row of matrix , assign the identifier of the last row of matrix to the first row of matrix , and so on so forth.

My code below tries to do this by calculating the absolute value between each row of and all the rows of 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 are computed the same as of .

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.

ThanksHi all,

I have two matrices: and . Each row of matrix has an identifier (index). I want to get the identifiers of each row of matrix based on how much the values in each row are close to each row of matrix . For example, if the values in the first row of matrix is the closest to those in the last row of matrix , assign the identifier of the last row of matrix to the first row of matrix , and so on so forth.

My code below tries to do this by calculating the absolute value between each row of and all the rows of 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 are computed the same as of .

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 Hi all,

I have two matrices: and . Each row of matrix has an identifier (index). I want to get the identifiers of each row of matrix based on how much the values in each row are close to each row of matrix . For example, if the values in the first row of matrix is the closest to those in the last row of matrix , assign the identifier of the last row of matrix to the first row of matrix , and so on so forth.

My code below tries to do this by calculating the absolute value between each row of and all the rows of 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 are computed the same as of .

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 matlab, indices, matrices, minimum, optimisation MATLAB Answers — New Questions