Filter cell array of objects
2 views (last 30 days)
Show older comments
I have a .net array object where each object is called Centroid. It has the following structure.
ans =
Centroid with properties:
x: 112.5769
y: 29.5762
count: 1250
strength: 12.3399
ans =
Centroid with properties:
x: 21.5000
y: 18.0690
count: 58
strength: 12.3400
I would like to create a cell array that only a cell for each of these objects. But I only want the ones that are greater than zero. Since there are 5 million of these. The following code works, but is too slow (stepper is the .net object that returns the Centroids).
ctroids = this.stepper.centers;
out = {};
j = 0;
for i = 1:ctroids.Length;
if (ctroids(i).count >0)
j = j+1;
out{j} = ctroids(i);
end
end
0 Comments
Answers (2)
Guillaume
on 4 Mar 2015
Wouldn't this work:
allctroids = cell(this.stepper.centers); %convert .Net Array into cell array
filteredctroids = allctroids(cellfun(@(c) c.count > 0, allctroids));
Jan
on 4 Mar 2015
Edited: Jan
on 4 Mar 2015
Try to pre-allocate the output:
ctroids = this.stepper.centers;
out = cell(1, ctroids.Length);
j = 0;
for i = 1:ctroids.Length;
if (ctroids(i).count >0)
j = j+1;
out{j} = ctroids(i);
end
end
out = out(1:j);
Please explain, what "too slow" exactly means. It matters if it takes days and you need minutes, of if you talk about a real-time processing.
2 Comments
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!