필터 지우기
필터 지우기

find the minimal value of a vector

조회 수: 1 (최근 30일)
Chaoyang Jiang
Chaoyang Jiang 2018년 4월 30일
댓글: Chaoyang Jiang 2018년 5월 1일
I wanna find the minimal value of column b with logical c of vector a. When b is super big , max operation takes a very long time(as shown in fig below). How to vectorize the for loop to reduce the calling times of max (the simplified code is shown below)?
a=[1 3 4 52];
b=[2 3 4];%column index
c=logical([1 0 1;1 1 0; 0 0 1]);
d=zeros(length(c),1);
for i=1:length(b)
d(i)=max(a(b(c(i,:))));
end

채택된 답변

Walter Roberson
Walter Roberson 2018년 4월 30일
No, there is no faster version. We discussed this already in one of your previous questions where you were asking about max(). As I said then in https://www.mathworks.com/matlabcentral/answers/397675-is-there-a-faster-version-of-min#comment_561596
"If you break out the timing, you will likely find that the cost is in extracting the sub-array c(1,d) to send to the min() function. min() is at worst a linear operation (I say at worst because in theory it could be run in parallel for sufficiently large arrays.)"
If you calculate
>> 9925.39/14867689025
ans =
6.6758122148711e-07
You can see that it is taking about 6.6E-7 seconds per iteration. That is twice as fast as just making one anonymous function call:
>> timeit(@() fun(),0)
ans =
1.441973e-06
so that is really pretty efficient; the Just In Time compiler must be doing a really good job with it.
  댓글 수: 1
Chaoyang Jiang
Chaoyang Jiang 2018년 5월 1일
Thanks a lot for your answer!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by