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
"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...
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?
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).
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.