From: Anthony Hopf on
Bruno,

I hate to rehash this, but the code I am using now is inefficient (and you mentioned that this would be a more effective way to bin my data). I have a 3d matrix or values indexed by r,theta,phi (each are 3d matrices). I would like to bin this 3d value matrix conditioned on the three other matrices (r, theta, and phi). On the matrix r I would like to collect the index values binned by a binning vector:

r_edge = -(binsize/2):binsize:(N*binsize)-(binsize/2);

can I do this with histc? It looks like I need to reshape the 3d matrix r into a vector to do the binning with histc (or maybe I don't and I just don't understand the indexing in matlab, if I reshape does matlab remember the old index values that will correspond to the 3d value matrix?)

I will then bin on theta and phi:

theta_edge = -(tbin/2):tbin:(M*tbin)-(tbin/2);
phi_edge = -(pbin/2):pbin:(P*pbin)-(pbin/2);

in this case I was that I would use your procedure with histcn, but again it looks like I need to reshape my theta and phi 3d matrices and don't want to lose the corresponding index values.

I'm not trying to be laze ( i just feel like I should "stand on the shoulders of giants" by accessing your understanding of these functions)

Thank you,

Anthony

"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <hrrp93$4ok$1(a)fred.mathworks.com>...
> "Anthony Hopf" <anthony.hopf(a)gmail.com> wrote in message <hrronl$sds$1(a)fred.mathworks.com>...
> > "Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <hr8lfl$3d9$1(a)fred.mathworks.com>...
> > > "Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message
> > > > %% New engine using HISTCN and ACCUMARRAY
> > > > % FEX: http://www.mathworks.com/matlabcentral/fileexchange/23897
> > > >
> > > > phi_s = 0.5*deltaphi_s:deltaphi_s:(2000-(0.5*deltaphi_s));
> > > > r_s = 0.5*deltar_s:deltar_s:(4000-(0.5*deltar_s));
> > > >
> > > > phir = [r_ss(:) phi_ss(:)];
> > > > [count edges mid loc] = histcn(phir, r_s, phi_s);
> > > > idx = all(loc,2);
> > > > val_s = accumarray(loc(idx,:), val_ss(idx));
> > > >
> > >
> > > Sorry, it might be necessary to make a slight adjustment of the edges as following:
> > >
> > > %%
> > > %Resample the data
> > > deltar_s = 100;
> > > deltaphi_s = 1;
> > >
> > > phi_s = 0:deltaphi_s:2000;
> > > r_s = 0:deltar_s:4000;
> > >
> > > phir = [r_ss(:) phi_ss(:)];
> > > [count edges mid loc] = histcn(phir, r_s, phi_s);
> > > val_s = accumarray(loc, val_ss(:), [length(r_s) length(phi_s)]-1);
> > >
> > > Bruno
> >
> > Bruno,
> >
> > In order to use this procedure the data would need to have a common delta in each direction correct?
>
> No, it's not correct. The (linear) grid is required to be strictly monotonic, that's all. There is no requirement to be a regular grid ("common delta" as you put it).
>
> Bruno
From: Anthony Hopf on
Here is the exact code I am using. My output is a sparse matrix (to conserve memory... there are many zeros... I use ~10% of the array for indexing)

function [ index_range ] = range_bin_sparse( r_4d, bin_size )
%RANGE_BIN Identifies the index values in r_4d that are within a bin of a given size
% each radar is done individually and the ouput file is a sparse
% matrix
% [ range_index ] = range_bin_sparse( r_4d, bin_size )

%single range bin binning of a single radar
r_bin_max = ceil((((nanmax(r_4d(:)))))/bin_size);
r_bin = (bin_size/2:bin_size:r_bin_max*bin_size+bin_size/2);%[m]
index_range = zeros(length(r_bin),250);

%filter variables
rH = r_bin + bin_size/2;
rL = r_bin - bin_size/2;
%find exceptable values for each range bin
for v=1:length(r_bin)
index_temp = find((r_4d) > rL(v) & (r_4d) <rH(v));
%if max_index < length(index_range)
%max_index = length(index_range);
%end
index_range(v,1:length(index_temp)) = index_temp;
%clear index_temp;
if isempty(index_temp)
v = length(r_bin);
end
clear index_temp;
end
index_range = sparse(index_range');

end


"Anthony Hopf" <anthony.hopf(a)gmail.com> wrote in message <hul738$b2p$1(a)fred.mathworks.com>...
> Bruno,
>
> I hate to rehash this, but the code I am using now is inefficient (and you mentioned that this would be a more effective way to bin my data). I have a 3d matrix or values indexed by r,theta,phi (each are 3d matrices). I would like to bin this 3d value matrix conditioned on the three other matrices (r, theta, and phi). On the matrix r I would like to collect the index values binned by a binning vector:
>
> r_edge = -(binsize/2):binsize:(N*binsize)-(binsize/2);
>
> can I do this with histc? It looks like I need to reshape the 3d matrix r into a vector to do the binning with histc (or maybe I don't and I just don't understand the indexing in matlab, if I reshape does matlab remember the old index values that will correspond to the 3d value matrix?)
>
> I will then bin on theta and phi:
>
> theta_edge = -(tbin/2):tbin:(M*tbin)-(tbin/2);
> phi_edge = -(pbin/2):pbin:(P*pbin)-(pbin/2);
>
> in this case I was that I would use your procedure with histcn, but again it looks like I need to reshape my theta and phi 3d matrices and don't want to lose the corresponding index values.
>
> I'm not trying to be laze ( i just feel like I should "stand on the shoulders of giants" by accessing your understanding of these functions)
>
> Thank you,
>
> Anthony
>
> "Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <hrrp93$4ok$1(a)fred.mathworks.com>...
> > "Anthony Hopf" <anthony.hopf(a)gmail.com> wrote in message <hrronl$sds$1(a)fred.mathworks.com>...
> > > "Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <hr8lfl$3d9$1(a)fred.mathworks.com>...
> > > > "Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message
> > > > > %% New engine using HISTCN and ACCUMARRAY
> > > > > % FEX: http://www.mathworks.com/matlabcentral/fileexchange/23897
> > > > >
> > > > > phi_s = 0.5*deltaphi_s:deltaphi_s:(2000-(0.5*deltaphi_s));
> > > > > r_s = 0.5*deltar_s:deltar_s:(4000-(0.5*deltar_s));
> > > > >
> > > > > phir = [r_ss(:) phi_ss(:)];
> > > > > [count edges mid loc] = histcn(phir, r_s, phi_s);
> > > > > idx = all(loc,2);
> > > > > val_s = accumarray(loc(idx,:), val_ss(idx));
> > > > >
> > > >
> > > > Sorry, it might be necessary to make a slight adjustment of the edges as following:
> > > >
> > > > %%
> > > > %Resample the data
> > > > deltar_s = 100;
> > > > deltaphi_s = 1;
> > > >
> > > > phi_s = 0:deltaphi_s:2000;
> > > > r_s = 0:deltar_s:4000;
> > > >
> > > > phir = [r_ss(:) phi_ss(:)];
> > > > [count edges mid loc] = histcn(phir, r_s, phi_s);
> > > > val_s = accumarray(loc, val_ss(:), [length(r_s) length(phi_s)]-1);
> > > >
> > > > Bruno
> > >
> > > Bruno,
> > >
> > > In order to use this procedure the data would need to have a common delta in each direction correct?
> >
> > No, it's not correct. The (linear) grid is required to be strictly monotonic, that's all. There is no requirement to be a regular grid ("common delta" as you put it).
> >
> > Bruno