Filter cell array of objects

1 view (last 30 days)
Doctor G
Doctor G on 4 Mar 2015
Edited: Doctor G on 6 Mar 2015
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

Answers (2)

Guillaume
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));
  1 Comment
Doctor G
Doctor G on 6 Mar 2015
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[]]

Sign in to comment.


Jan
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
Doctor G
Doctor G on 6 Mar 2015
Edited: Doctor G on 6 Mar 2015
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 on 6 Mar 2015
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

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!