Matlab recursively add points to a vector

3 views (last 30 days)
Hi, I have a 3d matrix and i wish to create a function that looks at its enviroment and if a point in that enviroment satisfies a condition i want to add it to my vector. then use the function on that point to see if it also satisfies and so on. my idea was to preform a recursive function that calls itself on the next point that satisfies the condition. the only problem is that for every point i add to the vector Matlab would copy the entire vector array, this will slow me down incredibly as that vector is incredibly large, from what i understand passing a pointer to the array is impossible in matlab so im confused as to how am i to do it?
my question is mostly how do i pass an array of vectors as pointer so that matlab keeps feeling it instead of re-creating it over and over again.
  1 Comment
Jan
Jan on 17 May 2017
I do not understand the detail concerning "looks at its environment". What does this mean? What exactly is a "vector array"? A matrix? Matlab uses a copy-on-write strategy, such that you do not create a deep data copy, when a variable is provided as input to a function. Only the iterative growing of arrays is a problem. Terms like "incredibly large" are not useful in a scientific discussion. Some people are impressed by 10'000 elements already, because they cannot add them manually anymore. Other users run Matlab on huge TB cluster machines.

Sign in to comment.

Accepted Answer

Matt J
Matt J on 17 May 2017
Edited: Matt J on 17 May 2017
I would recommend a different approach entirely. I would first evaluate every voxel (i,j,k) in the array to see if it satisfies the condition. This will give you a 3D binary map BW. Then, I would use bwconncomp or regionprops in the Image Processing ToolBox to find the groups of 'on' voxels in BW that are connected through a chain of neighbors.
There are many possible variations on this idea that could be applicable if you give a more detailed description of the task.
  3 Comments
Daniel ziv
Daniel ziv on 17 May 2017
actually it seems very possible if i locally threshold my cells to turn them into binary and then use connected_components and then check if my seed hits the connected component. I find it hard to find all the methods nessasary if its simple enough can u plz help me with those few lines of code?
Image Analyst
Image Analyst on 19 May 2017
You forgot to attach the image. Even a 2-D slice would help me to understand. I'm inclined to agree with Matt J so far, but then, we haven't seen your image yet.

Sign in to comment.

More Answers (1)

Jan
Jan on 17 May 2017
Edited: Jan on 19 May 2017
See Lorens Blog: Inplace operations . Maybe your assumption is wrong, that Matlab copies the "vector array" (what ever this means) in each call of a function. If you explain, how many elements "incredibly large" is, one could estimate, if a cell array is a useful solution for you. So please add more details and explain, why you think that the memory management is the bottleneck of your code.

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!