Filter cell array of objects
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Guillaume
2015-3-4
Wouldn't this work:
allctroids = cell(this.stepper.centers); %convert .Net Array into cell array
filteredctroids = allctroids(cellfun(@(c) c.count > 0, allctroids));
Jan
2015-3-4
编辑:Jan
2015-3-4
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.
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!