Using randsample on a matrix of weights
Mostra commenti meno recenti
Hi,
I have some code like this:
for k = 1:p
new_index = randsample([1:n,nan],1,true,tot_weights(k,:));
curr_indexes(k) = new_index;
if ~isnan(new_index)
currx(k) = x(i,new_index);
curry(k) = y(i,new_index);
else
currx(k) = nan;
curry(k) = nan;
end
end
where tot_weights is a p x n matrix that has the weights for the sampling. I am trying to get rid of the for loop and vectorize the function. Basically I just want to get a total of p values that are between 1 to n or nan. The probability for the kth value to be a certain value is given by the weights in the kth row of tot_weights.
Any suggestions would be greatly appreciated! Right now, this for loop takes an annoying amount of time to run.
Cheers,
Sam
2 Commenti
Jeff Miller
il 5 Lug 2020
The weights seem to change for different values of k, so I'd guess you need to build curr_indices in a for loop. Seems like you could move everything else outside the for loop and vectorize that part of it, though.
Might be a little quicker to replace [1:n,nan] with [1:n+1] and treat n+1 as a special index pointing to nan.
Samuel Moor-Smith
il 5 Lug 2020
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Logical in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!