Filter cell array of objects

2 次查看(过去 30 天)
Doctor G
Doctor G 2015-3-4
编辑: Doctor G 2015-3-6
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

回答(2 个)

Guillaume
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));
  1 个评论
Doctor G
Doctor G 2015-3-6
allctroids = cell(this.stepper.centers);
Error using cell
Conversion to cell from PipeLine.Centroid[] is not possible.
Here is the defenition of centers:
centers: [1x1 PipeLine.Centroid[]]

请先登录,再进行评论。


Jan
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.
  2 个评论
Doctor G
Doctor G 2015-3-6
编辑:Doctor G 2015-3-6
It was taking about 10 minutes, on a 64 bit, 4 core, alienware A51. (Windows 7). I will try this out and get back to you. I don't need real time, but under a minute would be much better. Best is under 10seconds.
Doctor G
Doctor G 2015-3-6
I implemented your code as part of my clean() method. At the end is some work to get just the (x,y) pairs out of the cells (which I need for the plot shown afterwards, though I think this might also be simplified). But the bad news is that the issue is not about pre-allocation. The tic/toc timeing came out at 588.5401 which is 9.8 minutes.
function clean(this)
tic;
ctroids = this.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
this.cleanTime = toc;
out = out(1:j);
for i = 1:j
this.cx(i) = out{i}.x+1;
this.cy(i) = out{i}.y+1;
end
end
function plot(this)
hold on
this.show(this.mask);
plot(this.cx, this.cy, 'r.');
hold off
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Getting Started with Microsoft .NET 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by