Using randsample on a matrix of weights

8 次查看(过去 30 天)
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 个评论
Jeff Miller
Jeff Miller 2020-7-5
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
Yeah, you're right! I moved everything else outside and it works a little faster. Thank you!

请先登录,再进行评论。

采纳的回答

Sindar
Sindar 2020-7-5
编辑:Sindar 2020-7-5
it doesn't seem like the assignment statement has any reason to be in the loop. Pulling it out may save a decent amount of time (especially if nan is common):
% sample each row of tot_weights
% there may be a direct way to sample a weight matrix like this loop-free, but I don't know it
for k = 1:p
curr_indexes(k) = randsample([1:n,nan],1,true,tot_weights(k,:));
end
% set currx and curry to all-nans of the correct size
currx = nan(1,p);
curry = nan(1,p);
% update currx where curr_indexes is not nan
% fill these with curr_indexes of x (excluding places where curr_indexes is nan)
currx(~isnan(curr_indexes)) = x(i,curr_indexes(~isnan(curr_indexes)))
% same for curry
curry(~isnan(curr_indexes)) = y(i,curr_indexes(~isnan(curr_indexes)));
  1 个评论
Samuel Moor-Smith
Yeah, you're right! I moved everything else outside and it works a little faster. Thank you!

请先登录,再进行评论。

更多回答(0 个)

类别

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