Need to accelerate an operation on a matrix
Show older comments
I'm writing code that involves looping through each element in arrays with appx. 10e5 elements. For each value in the array, a filter is applied to the cells around it. This filtered neighborhood is used to determine the value assigned to a value in a new matrix. This process is the bottleneck in the code, so I'd appreciate any help in making this loop faster. I tried a gpuArray-based implementation but it was significantly slower than what I'm currently doing. One thing I find curious is that a simple indexing of the large array takes longer than some of the more complex operations.
Here's the code:
for k = 1:ncells
pos = allinds(k,:);
yf = pos(1) + sensdist;
xf = pos(2) + sensdist;
% Slow operation: apply the neighborhood filter
neighbors = subfield(yf-sensdist:yf+sensdist, xf-sensdist:xf+sensdist) .*obj.nhood;
%Slow operation: look up next value for this index
nextstate = trans(1,subfield(yf,xf));
%Slow operation: check how many neighbors possess are the next value
quorum = sum(sum(neighbors == nextstate));
if quorum > 1;
if ~isempty(zipintersect(quorum,obj.go))
temp(k) = nextstate;
end
end
end
4 Comments
How big is sensdist, and hence the neighborhoods? How big is ncells as compared to numel(subfield)?
One thing I find curious is that a simple indexing of the large array takes longer than some of the more complex operations.
There's nothing "simple" about indexing arrays. Getting things in and out of memory is always one of the most troublesome operations to perform, especially when jumping around nonsequentially in the array, as you're doing here.
Davis
on 28 Jan 2013
Matt J
on 28 Jan 2013
What about ncells? How big is that?
Davis
on 4 Feb 2013
Accepted Answer
More Answers (0)
Categories
Find more on Neighborhood and Block Processing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!