필터 지우기
필터 지우기

Need help with a While loop

조회 수: 1 (최근 30일)
Nicolo Zaza
Nicolo Zaza 2012년 7월 19일
So I'm trying to write a rainflow counting program and I'm having trouble with the repetitions
while i <= numel(PV) ;
i = i +1;
N = N+ 1;
PV(N);
switch N
case 3
X = abs(PV(N) - PV(N-1))
Y = abs(PV(N-1) - PV(N-2))
case 4
X = abs(PV(N) - PV(N-1))
Y = abs(PV(N-1) - PV(N-2))
end
end
Instead of doing cases all the way to 24 there must be an easier way but I suck at matlab and would appreciate any help. Thanks
  댓글 수: 1
Nicolo Zaza
Nicolo Zaza 2012년 7월 19일
What Im actually trying to do is to re arrange a previous code to make work with some other scripts I developed but it is in fortran and I'm having a hard time translating it.
DIMENSION E(50)
N=0
1 N=N+1
CALL DATA(E(N),K)
IF(K == 1) STOP
2 IF(N < 3) GO TO 1
X=ABS((E(N)-E(N-1))
Y=ABS(E(N-1)-E(N-2))
3 IF(X.LT.Y) GO TO 1
4 RANGE=Y
XMEAN=(E(N-1)+E(N-2))/2
N=N-2
E(N)=E(N+2)
GO TO 2
END

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

채택된 답변

Kevin Claytor
Kevin Claytor 2012년 7월 19일
편집: Kevin Claytor 2012년 7월 19일
You may want to have a look at this file to see how they implement the rainflow method; http://www.mathworks.com/matlabcentral/fileexchange/3026
Now I'm just going to take a stab at my interpretation and re-writing of your FORTRAN code (added matlab-style comments to the end);
DIMENSION E(50) % Not sure what this does; same as E=[1:50]?
N=0 % Initalize counters
1 N=N+1
CALL DATA(E(N),K) % K = DATA(E(N))
IF(K == 1) STOP % Throw an error if the first point is 1?
2 IF(N < 3) GO TO 1 % Can't do algorithm for N < 3, increment N
X=ABS((E(N)-E(N-1)) % Compute X and Y
Y=ABS(E(N-1)-E(N-2))
3 IF(X.LT.Y) GO TO 1 % Continue if X > Y - this is the 'flow part'
4 RANGE=Y % Set some other parameters
XMEAN=(E(N-1)+E(N-2))/2
N=N-2 % Go back to see if this is still max
E(N)=E(N+2) % Update counter with prev max at location N
GO TO 2
END
So to me, and I'm likely wrong, as I've never done rainflow counting before and just read the wiki page on it now in about 15 secs...
DATA = [...blargh...];
LDATA = length(DATA);
E = [1:LDATA]; % A linear index of where our flows start from?
N = 3; % no sense starting before the algorithm can kick in
while N < LDATA
X = abs(DATA(N) - DATA(N-1));
Y = abs(DATA(N-1) - DATA(N-2));
if x > Y
RANGE = Y;
XMEAN = (DATA(N-1) + DATA(N-2))/2;
E(N-2) = E(N);
N = N-2;
else
N = N+1;
end
end
Maybe this helps?
  댓글 수: 1
Nicolo Zaza
Nicolo Zaza 2012년 7월 19일
This helped far more than what I was expecting.. Had to do some changes but it gave me a great idea.. Thanks a lot man.

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

추가 답변 (4개)

Walter Roberson
Walter Roberson 2012년 7월 19일
Why have a switch at all? Why not just
if N >= 3
X = abs(PV(N) - PV(N-1))
Y = abs(PV(N-1) - PV(N-2))
end
  댓글 수: 2
Andrei Bobrov
Andrei Bobrov 2012년 7월 19일
if N >= 3 && N <= 4
X = abs(PV(N) - PV(N-1))
Y = abs(PV(N-1) - PV(N-2))
end
Albert Yam
Albert Yam 2012년 7월 19일
Following from Nicolo's response about keeping X,Y values.
for N = 3:length(PV)
if N >= 3 && N <= 4
X(N-2) = abs(PV(N) - PV(N-1))
Y(N-2) = abs(PV(N-1) - PV(N-2))
end
end
but at this point.. Walter is right .. why loop?
X = PV(3:end);
Y = PV(3:end);
X(1) = abs(PV(3) - PV(2));
X(2) = abs(PV(2) - PV(1));
Y(1) = abs(PV(3) - PV(2));
Y(2) = abs(PV(2) - PV(1));

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


Matt Kindig
Matt Kindig 2012년 7월 19일
편집: Matt Kindig 2012년 7월 19일
I reformatted the code for easier reading. A few questions:
  1. 1. what are X and Y for? They don't appear to be used anywhere in the loop, and are overwritten each time.
  2. 2. For N=3 or N=4, X and Y are defined in the same way.
  3. 3. Are i and N initialized prior to the loop?
  4. 4. What exactly is this code supposed to do? If we understand your intended output more clearly, we can probably recommend a more direct approach.
Your code:
while i <= numel(PV)
i = i +1;
N = N+ 1;
PV(N);
switch N
case 3
X = abs(PV(N) - PV(N-1))
Y = abs(PV(N-1) - PV(N-2))
case 4
X = abs(PV(N) - PV(N-1))
Y = abs(PV(N-1) - PV(N-2))
end
end
  댓글 수: 1
Nicolo Zaza
Nicolo Zaza 2012년 7월 19일
편집: Jan 2012년 7월 19일
This is just a part of the code Im trying to write, I actually have a fortran-like version of it if it would be of any help. The thing is that case 3 and 4 are not the only ones. What Im trying to do is for the loop to continue until theres no more values
PV = [ 80 -40 60 0 60 0 80 -100 60 20 60 20 80 -100 40 -80 0 -20 ...
0 -20 40 -80 80 -40];
SA = length(PV);
N = 0;
i = 1;
while i <= numel(PV) ;
i = i +1;
N = N+ 1;
PV(N);
switch N
case 3
X = abs(PV(N) - PV(N-1))
Y = abs(PV(N-1) - PV(N-2))
case 4
X = abs(PV(N) - PV(N-1))
Y = abs(PV(N-1) - PV(N-2))
end
end
This is what i have until now, The program is suppose to array the values from max to mins in a way defined by Rainflow Counting Algorithms(not sure if you've heard of it.) I could provide the fortran code if it would be of any help.
[EDITED, code formatted, Jan] Please learn how to format the code by your own: empty line above and below the code, mark code, hit "{} code" button or insert two spaces in front of each line manually. It is not so hard.

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


Andrei Bobrov
Andrei Bobrov 2012년 7월 19일
편집: Andrei Bobrov 2012년 7월 19일
switch N
case {3,4}
X = abs(PV(N) - PV(N-1));
Y = abs(PV(N-1) - PV(N-2));
end
OR
k = abs(diff(PV(1:4)));
xy = flipud(k(hankel(1:2,2:3)))
X = xy(1,N-2);
Y = xy(2,N-2);

Nicolo Zaza
Nicolo Zaza 2012년 7월 19일
Well yeah I already checked the Rainflow Method your sending me a link to but it doesn't seem to work properly. The problem I have is that I can't get it counting. I tried your code but it kept going and only displayed 1 answer for each element. It totally looks much better than mine. I need to get a bunch of X and Y's for 24 elements i should get around 12 of those(and for every other variable, ex: xmean) that's where I get lost. How can I keep it going and outputting results until there is an index with no info. And need all the results, like a vector. Also why cant an algorithm be done for {If N<3 , N = N + 1,end} something like that? Thanks
  댓글 수: 1
Albert Yam
Albert Yam 2012년 7월 19일
The reason you only get 1 set of X, Y, is because you tell Matlab to over write it. This is the first instance of you mentioning that you need to keep previous X, Y values.
for N=1:length(PV)
if N<3
%stuff
end
end

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

카테고리

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