ismember runs too slow (others have asked similar questions)

조회 수: 7 (최근 30일)
Nathaniel Werner
Nathaniel Werner 2018년 8월 15일
댓글: Nathaniel Werner 2018년 8월 15일
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
dpb
dpb 2018년 8월 15일
"... profiled ... code [and] largest ... total time spent is ... ismember [then] sortrows>sort_back_to_front. This is after preallocating the TimeAvg variable"
Just for comparison, edit the code to show the timed version so can see it and folks can use it if trying to help.
Also, just for comparison, how does the overall run time of the modified code with preallocation compare to that before--did that make noticeable improvement?
Yet again I go back to my previous comment as likely being the way to get more original ideas generated of alternate solutions.
Nathaniel Werner
Nathaniel Werner 2018년 8월 15일
Ok now I have compared the situations there is not any noticeable difference.
With preallocation.
Without preallocation.

댓글을 달려면 로그인하십시오.

채택된 답변

Greg Dionne
Greg Dionne 2018년 8월 15일
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)];
  댓글 수: 2
Nathaniel Werner
Nathaniel Werner 2018년 8월 15일
Thank you I will check this out and get back to you.
Nathaniel Werner
Nathaniel Werner 2018년 8월 15일
@Greg Dionne, this works very well thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by