I have two arrays, A and B, whose indices relate to inputs of an analysis (i.e. A(1) and B(1) are two tracked outputs of run 1 of a simulation with distinct inputs, A(7) and B(7) correspond to the 7th run, whose inputs vary from all other runs).
A B0.5 10.75 10.75 31 70.5 40.75 91 21 60.5 1
I am trying to create a vector of indices which tracks all minimum values of B for each unique value of A So from the above example the desired output is
index128 10
*Note that if there are multiple indices which have the minimum value of B for the same value of A, i want to keep both indices.
I am currently using a for loop (below), but because my array A and B are extremely large (1*10^8 entries) it take hours to run the script
%Extract unique elements of A
Unique_A = unique(A); %preallocate index vector for speed (no clue how big this will be so intentionally large
Alternative_index = zeros(1*10^6,1); for i = 1:length(Unique_A) % Find the indices of the Benefit vector which correspond to a each unique value of A
indices = find(A==Unique_A(i)); %Now determine which of the corresponding indices in B are equal to the minimum value
min_B_indices = (indices(B(indices)== min(B(indices)))) %since the index vector was pre allocated, I need to make sure I am appending the vector without overwriting entries. So determine how many entries will need to be appended:
length_of_append = length(min_B_indices) %Now find the first zero element of the preallocated vector
first_zero = find(Alternative_index==0, 1, 'first') %Now add the indices to the preallocated vector
Alternative_index(first_zero:first_zero+length_of_append-1,1) = min_B_indices; end %remove trailing zeros
Alternative_index = Alternative_index(Alternative_index~=0);
I have wracked my brain on how to do this without a for loop…anyone have a suggestion?
Best Answer