MATLAB Answers

Need help with a while loop to take numbers off.

Asked by Nicolo Zaza

Nicolo Zaza (view profile)

on 21 Jul 2012

Alright so I have counting algorithm but i need it to extract elements from an original source and after extracting them and storing I need matlab to discard them so they dont get count again. How do i do this? My code: if true

```DATA = [ 60 0 40 -20 40 -40 50 -30 30 -60 0 -30 40 -30 60];
L = length(DATA);
E = [1:L];
N = 1;
while N < L
N = N + 1;
if N < 3
continue
end
X(N) = [abs(DATA(N)-DATA(N-1))];
Y(N) = [abs(DATA(N-1)-DATA(N-2))];
if X < Y
continue
else
RANGE(N) = Y(N);
XMEAN(N) = [.5*(DATA(N-1)+DATA(N-2))]
DATA (N -2) = []
DATA (N-1) = []
N = N - 2
end
for I = N:L
L = L - 2
E(I) = E(I+1);
end
end
end
```

Nicolo Zaza

Nicolo Zaza (view profile)

on 21 Jul 2012

Sorry to not finish expressing my question. So what I'm using right now is if true

```DATA(N-2) = []
&
DATA(N-1) = []
end
to delete the values used to calculate RANGE because once range is calculated I dont need to calculate it again for those same points. Is there a better matlab function/code to accomplish this need?
```

Products

No products are associated with this question.

1 Answer

Image Analyst (view profile)

Answer by Image Analyst

Image Analyst (view profile)

on 21 Jul 2012
Edited by Image Analyst

Image Analyst (view profile)

on 21 Jul 2012

Yes, that will delete elements but you're indexing elements and that will change, not to mention the length of your DATA array keeps getting shorter as you delete elements to eventually as N approaches L you'll get an index out of range error.

What are you really trying to do? Can't you just extract arrays and subtract them, and use diff? Or use hist if you want to count values?

```data_n = DATA(3:end)
data_nm1 = DATA(2:end-1)
data_nm2 = DATA(1:end-2)
X = abs(data_n - data_nm1)
Y = abs(data_n - data_nm2)
RANGE = y
xMean = 0.5 * (data_nm1 - data_nm2)
% I have no idea what E is supposed to represent.
```

Nicolo Zaza

Nicolo Zaza (view profile)

on 21 Jul 2012

To be honest I do not know what E is representing. I am performing something called Rainflow Counting. So what I'm trying to do is to count one max value and min value (RANGE), then discard those points in the array and do it again for the secondmax second min do the same, thirdmax thirdmin and so on. everytime discarding the value already calculated

Image Analyst

on 21 Jul 2012
Nicolo Zaza

Nicolo Zaza (view profile)

on 21 Jul 2012

Yes I've seen it but is written in C and is hard to manipulate for me. I dont have much programming knowledge. Besides the program I need is pretty simple compared to that

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!