Thread Subject: Need to make code more Efficient

Subject: Need to make code more Efficient

From: Fraser Dickson

Date: 9 Feb, 2010 10:36:04

Message: 1 of 7

Hi i have a piece of code that looks through a data set and works out the average value for each cell based on the values sourrounding that cell ( 10 cells above and 10 cells below averaged). The data set is extremely large ( roughly 500 x 40) so i was wondering if anyone could reccomend how i can speed up my code. I have a lot of for loops and im sure there is nothing efficient about that !

Thanks

average = zeros(500,40);
 r1 = zeros(kd,1);
 r2 = zeros(kd,1);
   
 for x = 1:40
        for y = 1 : 500

            d_under_test = x+kd+cd;
            r_under_test = y +kr+cr;



            for counter = 1:kd
                r1(counter) = temp_array((d_under_test + cd +counter ),(r_under_test));
            end

            for counter = 1:kd
                r2(counter) = temp_array((d_under_test - cd -counter ),(r_under_test));
            end

            r2mean = mean(r2);
            r1mean = mean(r1);
            average(x,y) = 0.5*(r1mean+r2mean);
     end
    end

Subject: Need to make code more Efficient

From: Oleg Komarov

Date: 9 Feb, 2010 12:49:02

Message: 2 of 7

"Fraser Dickson"
> Hi i have a piece of code that looks through a data set and works out the average value for each cell based on the values sourrounding that cell ( 10 cells above and 10 cells below averaged). The data set is extremely large ( roughly 500 x 40) so i was wondering if anyone could reccomend how i can speed up my code. I have a lot of for loops and im sure there is nothing efficient about that !
>
> Thanks
>
> average = zeros(500,40);
> r1 = zeros(kd,1);
> r2 = zeros(kd,1);
>
> for x = 1:40
> for y = 1 : 500
>
> d_under_test = x+kd+cd;
> r_under_test = y +kr+cr;
>
>
>
> for counter = 1:kd
> r1(counter) = temp_array((d_under_test + cd +counter ),(r_under_test));
> end
>
> for counter = 1:kd
> r2(counter) = temp_array((d_under_test - cd -counter ),(r_under_test));
> end
>
> r2mean = mean(r2);
> r1mean = mean(r1);
> average(x,y) = 0.5*(r1mean+r2mean);
> end
> end

"The data set is extremely large ( roughly 500 x 40) ."
Huge is when you have a 100e6 by 20 matrix...

Plz post the result of:
whos kd
whos cd

You should definitely forget your approach. Read this useful doc:
http://www.mathworks.com/support/tech-notes/1100/1109.html

Oleg

Subject: Need to make code more Efficient

From: Fraser Dickson

Date: 9 Feb, 2010 13:08:02

Message: 3 of 7

Hi

cd = 2
  kd = 16

Basically the code looks at 16 values either side of the cell under test and returns the average ( ignoring the 2 cells ( kd) imeediate to the cell) so that will be (500*40) of doing this averaging routine

the code is taking at least 15 seconds to run through which is way to long i need to speed it up. I thought i was using vectorized loops could you explain what i am doing wrong?

Thanks

Subject: Need to make code more Efficient

From: Walter Roberson

Date: 9 Feb, 2010 15:42:36

Message: 4 of 7

Fraser Dickson wrote:
> Hi i have a piece of code that looks through a data set and works out
> the average value for each cell based on the values sourrounding that
> cell ( 10 cells above and 10 cells below averaged).

Sounds like a job for blkproc()

Subject: Need to make code more Efficient

From: Oleg Komarov

Date: 9 Feb, 2010 15:52:04

Message: 5 of 7

> I thought i was using vectorized loops could you explain what i am doing wrong?
>
> Thanks
Have you given a look at the link I posted?

Vectorized ~= Loop in matlab approach.

Oleg

Subject: Need to make code more Efficient

From: Fraser Dickson

Date: 9 Feb, 2010 20:12:18

Message: 6 of 7

I dont realy understand how i could do what i need to do without using a loop???

Ive never heard of this blcproc() func before i shall look into that

Subject: Need to make code more Efficient

From: Walter Roberson

Date: 9 Feb, 2010 22:29:27

Message: 7 of 7

Fraser Dickson wrote:

> for counter = 1:kd
> r1(counter) = temp_array((d_under_test + cd +counter
> ),(r_under_test));
> end
>
> for counter = 1:kd
> r2(counter) = temp_array((d_under_test - cd -counter
> ),(r_under_test));
> end

r2_r1 = temp_array(d_under_test - cd - kd : ...
                    d_under_test + cd + kd, ...
                    r_under_test);
r2mean = mean(r2_r1(1:kd));
r1mean = mean(r2_rw(end-kd-1:end));
average(x,y) = (r1mean + r2mean) / 2;


Note: because kr and cr do not vary according to y, you can vectorize to do an
entire column at one time.

Also note that you could move the initialization of d_under_test to outside of
the 'for y' loop.

> r2mean = mean(r2);
> r1mean = mean(r1);
> average(x,y) = 0.5*(r1mean+r2mean);
> end
> end

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com