Thread Subject: faster for loop

Subject: faster for loop

From: Michael Lonther

Date: 13 Nov, 2008 15:48:03

Message: 1 of 3

Hello,
I would greatly appreciate if there is a non-FOR-loop version of this code. It is very slow because I think it is very inefficient. Thank you..

% aggregate irr data to GADM values
fid = fopen('file1.bin','rb');
irrarea = fread(fid,'float');
fclose(fid);
clear fid;
fid = fopen('file2.bin','rb');
gadm = fread(fid,'float');
fclose(fid);
clear fid;

num = unique(sort(gadm));
num(1) = [];

irrmean = zeros(4320*2160,1);
for i = 1:length(num)
    a = find(gadm == num(i));
    irrmean(a) = mean(irrarea(a));
    clear a;
end

Subject: faster for loop

From: Gavrilo Bozovic

Date: 13 Nov, 2008 16:15:04

Message: 2 of 3

"Michael Lonther" <rsbu@yahoo.com> wrote in message <gfhi7j$str$1@fred.mathworks.com>...
> Hello,
> I would greatly appreciate if there is a non-FOR-loop version of this code. It is very slow because I think it is very inefficient. Thank you..
>
> % aggregate irr data to GADM values
> fid = fopen('file1.bin','rb');
> irrarea = fread(fid,'float');
> fclose(fid);
> clear fid;
> fid = fopen('file2.bin','rb');
> gadm = fread(fid,'float');
> fclose(fid);
> clear fid;
>
> num = unique(sort(gadm));
> num(1) = [];
>
> irrmean = zeros(4320*2160,1);
> for i = 1:length(num)
> a = find(gadm == num(i));
> irrmean(a) = mean(irrarea(a));
> clear a;
> end

some hints to improve it:

for i=1:length(num)
    irrmean(gadm==num(i))=mean(irrarea(a)) % no need for the *slow* find
end

Subject: faster for loop

From: Roger Stafford

Date: 13 Nov, 2008 20:19:03

Message: 3 of 3

"Michael Lonther" <rsbu@yahoo.com> wrote in message <gfhi7j$str$1@fred.mathworks.com>...
> Hello,
> I would greatly appreciate if there is a non-FOR-loop version of this code. It is very slow because I think it is very inefficient. Thank you..
>
> % aggregate irr data to GADM values
> fid = fopen('file1.bin','rb');
> irrarea = fread(fid,'float');
> fclose(fid);
> clear fid;
> fid = fopen('file2.bin','rb');
> gadm = fread(fid,'float');
> fclose(fid);
> clear fid;
>
> num = unique(sort(gadm));
> num(1) = [];
>
> irrmean = zeros(4320*2160,1);
> for i = 1:length(num)
> a = find(gadm == num(i));
> irrmean(a) = mean(irrarea(a));
> clear a;
> end

  Try the following to see if it is faster than your for-loop method. It assumes that 'gadm' and 'irrarea' are the same length - is that true? The resulting 'irrmean' will also be that length.

 [n,n,n] = unique(gadm); % Get mapping onto unique elements of sorted gadm
 m = accumarray(n,irrarea)./accumarray(n,1); % Get mean values of corres. irrarea
 m(1) = 0; % (Equivalent to the num(1)=[] step)
 irrmean = m(n); % Put them into matching irrmean

Roger Stafford

Tags for this Thread

Everyone's Tags:

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.

Tag Activity for This Thread
Tag Applied By Date/Time
for efficient l... Michael Lonther 13 Nov, 2008 10:50:22
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