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:
reducing process time

Subject: reducing process time

From: rohan

Date: 28 Oct, 2012 15:05:09

Message: 1 of 4

Is there any possible way to reduce the processing time taken to run this code, i am unable to think a way to get rid of these loops to reduce the time, the following is my code which works.

a=[48 160 160 0 -80 -32];
l=length(a);
for i=1:(l-2)
    b=a(l-i-1:l);
    lb=length(b);
    for j=3:lb
        b(j)=b(j)*0.5;
        b(j-2)=b(j-2)+b(j);
    end
    for k=1:lb
        a(l-lb+k)=b(k);
    end
    
end
b

I'm new to MATLAB, so I apologize if it's something trivially easy that I've missed.

Subject: reducing process time

From: Nasser M. Abbasi

Date: 28 Oct, 2012 15:47:54

Message: 2 of 4

On 10/28/2012 10:05 AM, rohan wrote:
> Is there any possible way to reduce the processing time taken to run this code, i am
>unable to think a way to get rid of these loops to reduce the time, the following
>is my code which works.
>
> a=[48 160 160 0 -80 -32];
> l=length(a);
> for i=1:(l-2)
> b=a(l-i-1:l);
> lb=length(b);
> for j=3:lb
> b(j)=b(j)*0.5;
> b(j-2)=b(j-2)+b(j);
> end
> for k=1:lb
> a(l-lb+k)=b(k);
> end
>
> end
> b
>
> I'm new to MATLAB, so I apologize if it's something trivially easy that I've missed.
>

First, do not use 'l' as variable. Hard to read from '1'. Use
L or someother letter.

It is better to say also what is it you are trying to do?
i.e. what is the code is supposed to modify 'a' to be? Are
you flipping order of elements somehow?

may be there is a function that allready does what you want.

Subject: reducing process time

From: Roger Stafford

Date: 29 Oct, 2012 01:04:08

Message: 3 of 4

"rohan " <rohan.samria@gmail.com> wrote in message <k6jhj4$esj$1@newscl01ah.mathworks.com>...
> Is there any possible way to reduce the processing time taken to run this code, i am unable to think a way to get rid of these loops to reduce the time, the following is my code which works.
> ......
- - - - - - - - - -
  See if this is any better, Rohan:

n = length(a);
b = zeros(size(a));
for i = 1:n-2
  ni = n-i+1;
  b(ni:n) = a(ni:n)/2;
  a(ni:n) = b(ni:n);
  a(ni-2:n-2) = a(ni-2:n-2)+b(ni:n);
end
a

(Note: with this code the end result lies in 'a' only, rather than in both 'a' and 'b'.)

Roger Stafford

Subject: reducing process time

From: Roger Stafford

Date: 29 Oct, 2012 03:03:07

Message: 4 of 4

  I just realized, 'b' isn't necessary at all. Just do this:

 a = [48 160 160 0 -80 -32];
 n = length(a);
 for i = 1:n-2
   ni = n-i+1;
   a(ni:n) = a(ni:n)/2;
   a(ni-2:n-2) = a(ni-2:n-2) + a(ni:n);
 end

(a = [98 140 40 -10 -10 -2])

Roger Stafford

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