Preallocation with object structures
조회 수: 3 (최근 30일)
이전 댓글 표시
Hello,
I have a question about preallocation with object-oriented programming.
I have this object:
This is the result of a few loops. I could have one only loop, but I prefere to do it step by step to better control that I'm not wasting my time with dumb errors. I think it's not important, but I'll show an example.
This is an object containing the tooth 11 (FDI nomenclature) of six different samples (STL-files) of teeth.
First I rotate and translate a tooth to fit a reference tooth (in this case tooth in second row). For this I get tform (transformation matrix) and movingReg (new set of points/vertices).
Then I do a nearest point analysis and get k and dist an calculate Fk which is the distance vector with which I calculate the MeanVertices for the reference tooth (that's why there is only one matrix, in the second row).
Matlab tells me to preallocate the whole time, which I don't know how to do with this object. How can I preallocate (for example) FDINumber11.Vertices (see the code bellow this paragraph) before reading the data and not knowing how many points the file has? I think solution would apply to the other characteristics.
for i=1:m % m is the amount of teeth, which is defined at the beginning
FDINumber11_STL = stlread(FDINumber11(i).STLPath);
FDINumber11(i).Vertices= FDINumber11_STL.Points;
FDINumber11(i).Faces = FDINumber11_STL.ConnectivityList;
end
The whole process takes a little while, not too much but I hope to one day try it with many more teeth, so optimization would be a good thing to do. The worst case is that I learn a good programming practice, so even in case I cannot preallocate here, I would be glad to get a good explanation of it.
As always thank you very much.
Diego
PS: I have an example of a preallocation here, used in this code and it seems to work.
summe1 = 0.0;
for i=1:m
summe1 = summe1 + (FDINumber11(i).Fk - FDINumber11(r).Vertices)/(m-1);
end
댓글 수: 0
채택된 답변
J. Alex Lee
2020년 9월 3일
I don't think you are understanding "preallocation" correctly based on your last example...matlab is [probably] asking you to "reserve space fo the m different stl files you want to process". In this case, m=6 if I understand your description. There is a simple fix:
for i = m:-1:1
FDINumber11_STL = stlread(FDINumber11(i).STLPath);
FDINumber11(i).Vertices= FDINumber11_STL.Points;
FDINumber11(i).Faces = FDINumber11_STL.ConnectivityList;
end
What this does is instead of go i = 1,2,3,4,5,6 it will go i = 6,5,4,3,2,1. This matters because
clear % clear all variables
x(6) = 1
is a way to implicitly tell matlab to preallocate 6 elements to the 1-D array x before assigning the 6th element. It works for structure arrays too.
It's also likely that you want to enapsulate all the analysis you do on an individual stl file into a single function, rather than do things in a loop with indices.
function results = stlAnalysis(stlpath)
% do stuff
end
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Whos에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!