Discover MakerZone

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

Thread Subject:
looking for help (I think its a basic MATLAB question)

Subject: looking for help (I think its a basic MATLAB question)

From: Shaun Hurley

Date: 7 Jul, 2010 03:38:06

Message: 1 of 4

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

Subject: looking for help (I think its a basic MATLAB question)

From: Matt Fig

Date: 7 Jul, 2010 03:50:12

Message: 2 of 4

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

Subject: looking for help (I think its a basic MATLAB question)

From: Roger Stafford

Date: 7 Jul, 2010 08:26:05

Message: 3 of 4

"Shaun Hurley" <jumpinghurley@yahoo.com> wrote in message <i10squ$fg9$1@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

Subject: looking for help (I think its a basic MATLAB question)

From: James Tursa

Date: 7 Jul, 2010 09:18:05

Message: 4 of 4

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i11dmt$1ee$1@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

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us