Question

I'm trying to vectorise the following MATLAB code to improve performance:

for IX = 2:NSCX
    for IY = 2:NSCY
        for IC = 1:Nc

                duplicate = 0;
                for IK = 1:IA(IX-1,IY-1,1)
                    if IA(IX-1,IY-1,IK+1) == IC
                        duplicate = 1;
                    end
                end
                if duplicate == 0
                    IA(IX-1,IY-1,1) = IA(IX-1,IY-1,1)+1;
                    IA(IX-1,IY-1,IA(IX-1,IY-1,1)+1) = IC;
                end
            end
        end
    end
end

IA is a 3-dimensional matrix (size(IA) = NSCX NSCY 9). Can anyone help with this. Thanks.

Was it helpful?

Solution

Well you should consider another way to go here. you should look at the function histc which finds the first value where the sample is larger that previous and smaller than the next sample. For you this can be done for each dimension. This will give you and index for each unsorted cell. I do not know what conditions applying for the +8 inhabitated cells, but I guess that the conditions can be applied after the sorting.

Example of histc in 1 dimension

a = [1 5 7 12];
b = 2:2:16;
[c,d] = histc(a,b)

d gives the index of b in which each a belongs and c is a logic of all non empty indices.

EDIT: Pseudo code for creating a regular meshgrid with histc

cellX = vector with all regular x centers (or borders?!)
cellY = vector with all regular y centers (or borders)
px = all unstructured x centers
py = all unstructured y centers
[~,xind] = histc(px,cellx); % which cellX does px belong too?
[~,yind] = histc(py,celly); % which cellY does py belong too?
% Code for properly taking care of indices to match wanted format
unstructuredPerStructured = accumarray(indVector, properInd,...) % find number of unstructured cells per structured.

I am still not sure of you output since your code is not runable and does not follow the wanted input/output standard of stack exchange, but this seems to be a good approach.

EDIT: Application of histc()

I have attempted to do something like you suggest but I'm not able to replicate the results of my previous code. I've posted the variables needed to run the code link.

NSCX = 20;
NSCY = 20;

cellX = reshape(Xsdc,numel(Xsdc),1);% vector with all regular x centers (or borders?!)
cellY = reshape(Ysdc,numel(Xsdc),1);% vector with all regular y centers (or borders)
px = eince(:,1);% all unstructured x centers
py = eince(:,2);% all unstructured y centers

[y1,i1] = sort(cellX);
[y2,i2] = sort(cellY);

[~,xind] = histc(px,y1); % which cellX does px belong too?
[~,yind] = histc(py,y2); % which cellY does py belong too?

xindrevert = i1(xind);
yindrevert = i2(yind);

for i = 1:Nc
    [a,b] = find(cellX == cellX(xindrevert(i)));
    [c,d] = find(cellY == cellY(yindrevert(i)));
    index = c.*NaN;
    for j = 1:length(c)
        check = a(a == c(j))';
        if ~isempty (check)
            index(j) = check;
        end
    end
    output(i) = index(~isnan(index));
end
unstructuredPerStructured = accumarray(output',1,[NSCX*NSCY,1]); % find number of unstructured cells per structured
unstructuredPerStructured = reshape(unstructuredPerStructured,NSCX,NSCY);

unstructuredPerStructured should equal IA(:,:,1), but does not. Can you see the issue? I'm guessing the difference is due to taking the cell centres.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top