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:
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

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