Need help with a While loop
古いコメントを表示
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
2012 年 7 月 19 日
採用された回答
その他の回答 (4 件)
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
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
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
2012 年 7 月 19 日
編集済み: Matt Kindig
2012 年 7 月 19 日
I reformatted the code for easier reading. A few questions:
- 1. what are X and Y for? They don't appear to be used anywhere in the loop, and are overwritten each time.
- 2. For N=3 or N=4, X and Y are defined in the same way.
- 3. Are i and N initialized prior to the loop?
- 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
2012 年 7 月 19 日
編集済み: Jan
2012 年 7 月 19 日
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
2012 年 7 月 19 日
0 投票
1 件のコメント
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
カテゴリ
ヘルプ センター および File Exchange で Fluid Dynamics についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!