From: Shaun Hurley on 6 Jul 2010 23:38 I am very new to MATLAB and looking for some help. If anyone could please help me I would really appreciate it. I'm trying to set up a program to do rainflow counting. I wanted to start with a very simple example and add more to it. At this point this is not considered rainflow counting. I want the program to go through the vector and eliminate one part of the vector at a time. When I set p=1 in the for-loop, I am trying to get the loop to start from the very beginning again, rather than increase the i. Any suggestions? Thanks in advance! -Shaun clc clear all x= [20 30 -40 50 20 50 30]; q=length(x)-1; % number of elements in x p=1; % starting with x(1) for i=p:q % loop from p to q if x(i)<=x(i+1) % if 1st number is <= to 2nd x(i)=[]; % erase 1st number x % recalculate x p=1; % start loop over (start at x(1) of new x's,) q=length(x)-1 % recalculate the number of elements in x end end
From: Matt Fig on 6 Jul 2010 23:50 Don't use the variable name i, as this masks MATLAB's bult-in function. Also, if you want to be able to manipulate the index of a running loop, you have to use a WHILE loop instead of a FOR loop. Does this do what you want? x= [20 30 -40 50 20 50 30]; q=length(x)-1; % number of elements in x p=1; % starting with x(1) while p<q % (or maybe p<=q) loop from p to q if x(p)<=x(p+1) % if 1st number is <= to 2nd x(p) = []; % erase 1st number p = 1; % start loop over (start at x(1) of new x's,) q = q - 1; % recalculate the number of elements in x else p = p + 1; end x % Show x at every iteration. end
From: Roger Stafford on 7 Jul 2010 04:26 "Shaun Hurley" <jumpinghurley(a)yahoo.com> wrote in message <i10squ$fg9$1(a)fred.mathworks.com>... > I am very new to MATLAB and looking for some help. If anyone could please help me I would really appreciate it. I'm trying to set up a program to do rainflow counting. I wanted to start with a very simple example and add more to it. At this point this is not considered rainflow counting. I want the program to go through the vector and eliminate one part of the vector at a time. When I set p=1 in the for-loop, I am trying to get the loop to start from the very beginning again, rather than increase the i. Any suggestions? Thanks in advance! > > -Shaun > > clc > clear all > > x= [20 30 -40 50 20 50 30]; > > q=length(x)-1; % number of elements in x > p=1; % starting with x(1) > > for i=p:q % loop from p to q > if x(i)<=x(i+1) % if 1st number is <= to 2nd > x(i)=[]; % erase 1st number > x % recalculate x > p=1; % start loop over (start at x(1) of new x's,) > q=length(x)-1 % recalculate the number of elements in x > end > end - - - - - - - It seems to me that you would save yourself a lot of grief by starting your for-loop at the far end. for k = length(x)-1:-1:1 if x(k) <= x(k+1) x(k) = []; end end That way when you do a delete it doesn't mess up the indexed positions of the elements yet to be tested and it requires only one pass through x. Roger Stafford
From: James Tursa on 7 Jul 2010 05:18 "Roger Stafford" <ellieandrogerxyzzy(a)mindspring.com.invalid> wrote in message <i11dmt$1ee$1(a)fred.mathworks.com>... > - - - - - - - > It seems to me that you would save yourself a lot of grief by starting your for-loop at the far end. > > for k = length(x)-1:-1:1 > if x(k) <= x(k+1) > x(k) = []; > end > end > > That way when you do a delete it doesn't mess up the indexed positions of the elements yet to be tested and it requires only one pass through x. All of the methods offered so far suffer from data copying. Every time you do the x(k) = [] operation MATLAB has to copy the entire dataset to a new memory location ... this is the same penalty you pay for increasing the size of an array incrementally in a loop, but here you are decreasing the size. To avoid this potentially massive time waste for large vectors, better to just remember the good spots and shrink all at once at the end. e.g., m = numel(x); y = true(m,1); % everybody starts out OK n = m; for k = m-1:-1:1 if x(k) <= x(n) y(k) = false; % this kth one gets deleted else n = k; % new max end end x = x(y); % only one data set copy here Comparing the for loop above (ltdelete) and the logical indexed loop (ltdelete2) gives, for example, >> z = floor(rand(1,100000)*100); >> x = z; >> tic;ltdelete;toc Elapsed time is 127.483428 seconds. >> x x = 99 97 96 65 >> x = z; >> tic;ltdelete2;toc Elapsed time is 0.004626 seconds. >> x x = 99 97 96 65 That's a lot of wasted time moving data around, and 100000 is only a moderately large array size. James Tursa
|
Pages: 1 Prev: Problem using hmmtrain Next: Weighted least square fr signal reconstruction |