MATLAB Answers

ismember runs too slow (others have asked similar questions)

20 views (last 30 days)
I am trying to use ismember in a long for loop to identify indices shared between arrays. This is pretty similar to a question that was asked here. Hopefully, an answer can be found for my question.
Below is my script that is taking far too long to run. I've included a sample of my workspace with the important variables here.
The main issue I'm encountering is that using ismember takes far too long. I've tried using parfor to see if that would help but it doesn't mitigate the problem much. If there is a faster way to make the second for loop run faster that would be the type of solution I'm looking for.
As suggested by those in the comments, I have profiled the section of my code in the second for loop. The largest amount of total time spent is from using ismember. The function with the largest self time is sortrows>sort_back_to_front. This is after preallocating the TimeAvg variable.
Lcu =length(count_uni);
for i=1:Lcu
count = count_uni(i);
k = find(count_all==count);
% count = 1: 1 unique values
% count = 2: 2 unique values
% etc.
PosAllk = PosAllrep(k,:);
A_allk = A_all(k); PVTr_allk = PVTr_all(k);
count_allk = count_all(k);
if count>1
LPAk = length(PosAllk);
for j=1:LPAk
b = ismember(PosAllrep,PosAllk(j,:),'rows');
Aavg = mean(A_all(b)); Pavg = mean(PVTr_all(b));
g = ismember(PosAlluni,PosAllk(j,:),'rows');
TimeAvg = [TimeAvg;[PosAlluni(g,:),Aavg,Pavg,count]];
end
else
TimeAvg = [PosAllk,A_allk,PVTr_allk,count_allk];
end
end
  12 Comments
Nathaniel Werner
Nathaniel Werner on 15 Aug 2018
Ok now I have compared the situations there is not any noticeable difference.
With preallocation.
Without preallocation.

Sign in to comment.

Accepted Answer

Greg Dionne
Greg Dionne on 15 Aug 2018
Looks like your positional data could fit in a 3-D histogram (168x24x168). This could be made considerably faster, but hopefully this will be enough to get you started:
load sample
xval = unique(PosAllrep(:,1));
yval = unique(PosAllrep(:,2));
zval = unique(PosAllrep(:,3));
A_i = discretize(PosAllrep(:,1),xval);
A_j = discretize(PosAllrep(:,2),yval);
A_k = discretize(PosAllrep(:,3),zval);
sz = [length(xval), length(yval), length(zval)];
subs = [A_i, A_j, A_k];
A_all_sum = accumarray(subs,A_all,sz);
PVTr_sum = accumarray(subs,PVTr_all,sz);
bin_cnt = accumarray(subs,1,sz);
A_mean = A_all_sum ./ bin_cnt;
PVTr_mean = PVTr_sum ./ bin_cnt;
ind = find(~isnan(A_mean(:)));
[ix,iy,iz] = ind2sub(sz,ind);
TimeAvg = [xval(ix) yval(iy) zval(iz) A_mean(ind) PVTr_mean(ind) bin_cnt(ind)];

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!