alternative form for find(max(x))

조회 수: 1 (최근 30일)
GCats
GCats 2020년 4월 4일
편집: dpb 2020년 4월 4일
Hi all!
I am trying to optimize my program by reducing the nested coding and perhaps "slow" functions such as find().
I have this:
gamma = 0.5
nodes = 5
if rand >= gamma
a = find(A(s(1),s(2),:) == max(A(s(1),s(2),:)),1);
else
a = randi(nodes);
end
A is a n x m x o matrix.
I was wondering if there's a different way to tackle this. I've looked at accum() but I don't seem to make it work properly. Thank you in advance!

채택된 답변

dpb
dpb 2020년 4월 4일
if rand >= gamma
[~,imx]=max(A(s(1),s(2),:);
a=imx;
else
a = randi(nodes);
end
  댓글 수: 2
Torsten
Torsten 2020년 4월 4일
Is it ensured that imx gives the index of the first occurence of the maximum within A(s1,s2,:) ?
dpb
dpb 2020년 4월 4일
편집: dpb 2020년 4월 4일
I've never caught it doing otherwise altho not sure it is documented...
ADDENDUM: Actually it is...
"If the largest element occurs more than once, then I contains the index to the first occurrence of the value."

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by