Filter löschen
Filter löschen

Preallocating for speed, what kind of variable is this?

2 Ansichten (letzte 30 Tage)
Recap
Recap am 26 Mär. 2016
Bearbeitet: dpb am 27 Mär. 2016
I'm looking through some code and I came across this loop where Box should be preallocated for speed. Box, is the variable that I should preallocate outside the loop. But I don't know what kind of a variable it is. Can anyone help by looking at this?
for posi = 1:Length
nArea=nBox(posi).Area; % Area of number/letters
nBox(posi).BoundingBox; % Frame objects
if nArea>50 && nArea<200 && ...
nBox(posi).BoundingBox(4)>10 && nBox(posi).BoundingBox(3)>7 && ...
nBox(posi).BoundingBox(3)<25
k=k+1;
Box(k,:)=round(nBox(posi).BoundingBox);
end
end

Akzeptierte Antwort

dpb
dpb am 26 Mär. 2016
Bearbeitet: dpb am 27 Mär. 2016
Box is just a 2D array of type whatever is nBox.BoundingBox (which would appear to be a floating point type given the round operation, else't it's a waste).
As for preallocating, the size isn't known a priori; only the number of rows that satisfy the condition will be allocated. Depending on the matnitude of the upper bound of the loop Length, the time savings may be trivial compared to the overhead of allocating a set, checking for overrunning and reallocating, etc., etc., etc., ...
You could, of course, allocate for the full Length rows and clear k+1:end after the loop finishes. The 2nd dimension is whatever is the size of the structure array.
It appears that the loop could be replaced if this is all there is by --
idx= isbetween(nBox.nArea,50,200) & ...
isbetween(nBox.BoundingBox(3),7,25) & ...
nBox.BoundingBox(4)>10;
Box=round(nBox(idx).BoundingBox);
where isbetween is my "syntactic sugar" utility function
>> type isbetwween
function flg=isbetween(x,lo,hi)
% returns T for values within range of input
% SYNTAX:
% [log] = iswithin(x,lo,hi)
% returns T for x between lo and hi values, exclusive
flg= (x>lo) & (x<hi);
>>
I've several of these that serve simply to reduce clutter at the upper level; another is iswithin which is inclusive on both bounds.
NB: The statement
nBox(posi).BoundingBox; % Frame objects
is pointless; perhaps it was a debug line if removed the trailing semi-colon to echo the values during development.
ERRATUM
As Guillaume reminds me, since LHS is an array instead of reduced structure arrray must contatenate the results--
Box=round(cat(1,nBox(idx).BoundingBox));
  2 Kommentare
Recap
Recap am 26 Mär. 2016
The range of Length will vary from 3 to 8. So is there no way to preallocate the Box since it will vary everytime?
dpb
dpb am 26 Mär. 2016
Well, yes, you can allocate Length elements but for only 8 max you'll not notice the difference unless this is buried deep in some nested construct.
But, as noted, throw away the loop and it'll all get allocated in "one swell foop" from the git-go and preallocating is then a non-issue.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Guillaume
Guillaume am 26 Mär. 2016
Bearbeitet: Guillaume am 26 Mär. 2016
As per dpb's answer, the loop and the if are completely unnecessary (and slow). Just use logical indexing on the whole array. As a result Box gets created in one go, when it receives the values and therefore there's no need to preallocate (as it would just slow down the code).
Now, dpb's answer is incorrect in the assignment to Box, but he's got the right idea. This will work:
areas = [nBox.Area];
bboxes = vertcat(nBox.BoundingBox);
Box = round(bboxes(areas > 50 & areas < 200 & bboxes(:, 4) > 10 ...
& bboxes(:, 3) > 7 & bboxes(:, 3) < 25, :));
Note that a comment explaining why the bounding box coordinates are rounded wouldn't go amiss. Assuming the coordinates come from regionprop, ceil would be more appropriate.
  3 Kommentare
Guillaume
Guillaume am 26 Mär. 2016
Only in missing a closing parenthesis
Not really. nBox is a structure array. So you've got to cat nbox.Area and nbox.BoundingBox.
dpb
dpb am 27 Mär. 2016
Hmmm...oy, yeah...forgotted it being an array as the target instead of a reduced structure array. OK, so I fixed that, too... :)

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by