필터 지우기
필터 지우기

Finding the maximum value in a loop

조회 수: 4 (최근 30일)
yasmin
yasmin 2012년 7월 23일
Hi all i am trying to find the maximum value in the loop below, can someone please help and explain
for i=1:100
for angle=-90:90;
phi=angle*pi/180;
xdata=[sin(sno+(phi))];
ydata=(m_mata(:,:,i))
R=(corrcoef(xdata,ydata));
end
example Result from R are
1.0000 0.6581
0.6581 1.0000
within the loop above i want to find the maximum value of R(2,1) and also the associated angle that is been used in xdata to give the maximum of R(2,1).
basically I am wanting to find the maximum of R(2,1)and also the associated angle that is been used in xdata to give the maximum of R(2,1),for each time I run for i=1:100, for each loop I want to store the value of R(2,1) and the compare the new value with previous value if the new value is greater for when i=2 then this will be the maximum of R(2,1).If the maximum value of R(2,1) when i=2 is smaller then we use the maximum of R(2,1) when i=1 At the end of the iteration I want to end up with a maximum value of R(2,1) and also the associated phi
I am a bit inexperienced with programming and not sure how to capture this concept and put it in Matlab.
Your help is much appreciated.
  댓글 수: 1
Andrei Bobrov
Andrei Bobrov 2012년 7월 23일
how the angle depends of xdata and ydata

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

답변 (1개)

Elizabeth
Elizabeth 2012년 7월 23일
편집: Elizabeth 2012년 7월 23일
I am a little confused on the code you've provided. Perhaps if you were to clarify the problem statement and your objective more clearly I could help me. With what I have understood from you, I have tried to issue you a solution. Here goes nothing, MATLAB code MATLAB code
i=100; % initialize i
R=zeros(2); %initialize R
for angle = -90:90
phi = angle*pi()/180
if i==100 % assuming you have x, y data spec when i=100
R1=corrcoef(xdata,ydata);
if R1(2,1)>R(2,1)
R=R1; PHA=phi;
% else R(2,1)>R1(2,1) so R=R;
end
end
end
Rmax=R; %largest R in Loop
PHI=PHA; %angle phi associated with largest R
  댓글 수: 1
yasmin
yasmin 2012년 7월 23일
Thank you Elizabeth
I have tried your code but it is returing the last value of the loop as the largest, which then gives me PHI of 90 which the last angle aswell. What am looking for is to be able to find the largest value of R1(2,1) and the associated angle within
R1=corrcoef(xdata,ydata);
Is it possible to get help with this.
once again thank you very much for your time.

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

카테고리

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