Vectorization using accumarray

134 views Asked by At

I want to project the texture of 3D surface (CylCoors 300000x3) into a 2D plane (Image 380x360). For doing so I take every unique value in Z (UniqueZ=unique(CylCoors(:,3))) and and Theta (UniqueTheta=unique(CylCoors(:,1))) and project all the texture values (PointValues 300000x1) where both meet like this:

Image=zeros(max(UniqueH),max(UniqueTheta)); %380x360

tic

HMat=bsxfun(@eq,CylCoors(:,3),UniqueH');  % 300000x380
ThetaMat=bsxfun(@eq,CylCoors(:,1),UniqueTheta'); %300000x360


for ii=1:length(UniqueH)          % Sloooow and not nice :(
    for jj=1:length(UniqueTheta)

        Image(ii,jj)=sum(PointValues.*...
            HMat(:,ii).*ThetaMat(:,jj))/...
            sum(HMat(:,ii).*ThetaMat(:,jj));

    end
end

toc

Here's an example with trimmed variables:

CylCoorsSample = [263.0000  184.2586   10.0000    
                  264.0000  183.0417   10.0000    
                  264.0000  182.1572   10.0000    
                  82.0000   157.4746   11.0000    
                  80.0000   158.2348   11.0000    
                  86.0000   157.3507   11.0000    
                  84.0000   157.7633   11.0000]   

PointValuesSample = [0.4745
                     0.5098
                     0.5020
                     0.4784
                     0.4510
                     0.4431
                     0.5804]


UniqueTheta = [80              
               82              
               84
               86
               263
               264]

UniqueH =[10
          11]

ThetaMat =                             HMat =   

 0     0     0     0     1     0       1   0
 0     0     0     0     0     1       1   0
 0     0     0     0     0     1       1   0
 0     1     0     0     0     0       0   1
 1     0     0     0     0     0       0   1
 0     0     0     1     0     0       0   1
 0     0     1     0     0     0       0   1 

Image =        % size: length(UniqueH)xlength(UniqueTheta)

   NaN       NaN       NaN       NaN    0.4745    0.5059
0.4510    0.4784    0.5804    0.4431       NaN       NaN

Is there a way to vectorize the for loops using accumarray? Something tells me this is the case, but I find the multidimensional sub indexing for this function quite confusing.

Any other vectorization solutions (using smart combinations of reshape,bsxfun, and sum I assume) are also welcome, but I would really like to understand this use of accumarray a bit better.

1

There are 1 answers

3
Dan On BEST ANSWER

Given:

CylCoorsSample = [263.0000  184.2586   10.0000    
                  264.0000  183.0417   10.0000    
                  264.0000  182.1572   10.0000    
                  82.0000   157.4746   11.0000    
                  80.0000   158.2348   11.0000    
                  86.0000   157.3507   11.0000    
                  84.0000   157.7633   11.0000]   

PointValuesSample = [0.4745
                     0.5098
                     0.5020
                     0.4784
                     0.4510
                     0.4431
                     0.5804]

You can use accumarray as follows (using the third output of unique to generate the required subs input):

[UniqueTheta, ~, subsTheta]=unique(CylCoorsSample(:,1))
[UniqueH,~,subsH]=unique(CylCoorsSample(:,3))
sz = [numel(UniqueH), numel(UniqueTheta)]

Image = accumarray([subsH, subsTheta], PointValuesSample, sz, @mean, NaN)