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에서 Performance and Memory에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by