필터 지우기
필터 지우기

How to vectorize this code with logical idexing

조회 수: 1 (최근 30일)
pietro
pietro 2014년 5월 14일
댓글: pietro 2014년 5월 15일
Hi all,
I have the following code, how can I avoid the for to make it faster?
a=[10:5:50];
b=[10:20:50];
c=rand(size(a));
for i=length(b)-1
d=c(a>=b(i) & a<b(i+1));
end
thanks
cheers
  댓글 수: 1
Jos (10584)
Jos (10584) 2014년 5월 15일
Since you're only storing the last values of d in each iteration, you can skip the for-loop completely:
i = length(b)-1
d = c(a>=b(i)) & a<b(i+1))
but I am pretty sure you intend to do something else.
What should happen with the values of d obtained in the iterations 1 to length(b)-2?

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

채택된 답변

Andrei Bobrov
Andrei Bobrov 2014년 5월 14일
편집: Andrei Bobrov 2014년 5월 15일
a=[10:5:50];
b=[10:20:50];
c=rand(size(a));
[~,ii] = histc(a,b);
d = accumarray(ii(:),c(:),size(b(:)),@(x){x})
  댓글 수: 3
Andrei Bobrov
Andrei Bobrov 2014년 5월 15일
corrected
pietro
pietro 2014년 5월 15일
It works. Thanks

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by