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