How could you find the index number of an answer output?

조회 수: 5 (최근 30일)
Chelsey
Chelsey 2011년 7월 18일
I have a code that looks like:
for ii=2:length(dep);
dz(ii) = dep(ii)-dep(ii-1);
drho(ii) = sigth(ii)-sigth(ii-1);
end
for ii=1:length(dz);
drhodz(ii) = drho(ii)/dz(ii);
end
max(drhodz)
Is there a way to go back and then see which ii this maximum answer corresponds to? (Does that question make sense? After finding the maximum drhodz I'm trying to work backwards to see at which dz (change in depth) this max occurs.)

채택된 답변

Arnaud Miege
Arnaud Miege 2011년 7월 18일
[max_value, max_idx] = max(drhodz)
The index ii corresponding to the maximum value max_value is given by max_idx.
HTH,
Arnaud

추가 답변 (2개)

Sean de Wolski
Sean de Wolski 2011년 7월 18일
Arnaud's reply is correct
To find the indices of multiple maxima:
find(drhodz==max(drhodz));
Also, your for-loops can be easily vectorized with:
dz = diff(dep);
drho = diff(sigth);
drhodz = dz./drho;
  댓글 수: 2
Chelsey
Chelsey 2011년 7월 18일
Thank you - a much more elegant solution than what I was trying to do.
Arnaud Miege
Arnaud Miege 2011년 7월 18일
Good point!!

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


Chelsey
Chelsey 2011년 7월 18일
Thank you both!

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by