如何以优雅的方式编写此代码(MATLAB 中的元胞数组和结构体)

发布于 2024-12-15 02:04:41 字数 941 浏览 0 评论 0原文

我想在 MATLAB 中绘制连接点

我的连接点来自 "stats" 的连接对象,其中每个 "stat" 来自 BW Regionprops 结构。

我编写的代码可以工作,但它有很多“丑陋”,即使在尝试了各种方法之后我也无法修复。

function plot_line( line )

a = cell2mat(line);
b = {a.Centroid};

matx = {};
maty = {};

for i = 1:size(b,2)
    matx{end+1} = b{i}(1);
    maty{end+1} = b{i}(2);
end

plot ( cell2mat(matx), cell2mat(maty) );

end

你能帮我让这段代码变得更好吗?这并不重要,因为我的代码运行良好,并且行数很短(<100 点),因此性能不是问题。

只是如果知道这个小函数应该如何以正确的方式编写,而不需要 for 循环和 3 次 cell2mat 调用,那就太好了。

在我的示例中:

  • line 是一个 <1xn cell>
  • line{1} 具有属性 'Centroid'
  • line{ i}.Centroid(1) 是 x 坐标,
  • line{i}.Centroid(2) 是 y 坐标。

实际上,我需要的只是为 i = 1:size( 绘制 line{i}.Centroid(1), line{i}.Centroid(2) line,2),但我不知道如何。

I would like to plot connected points in MATLAB.

My connected points come from connecting objects of "stats", where each "stat" comes from a BW regionprops struct.

The code I have written works, but it suffers from a lot of "ugliness", which I couldnt fix even after trying various ways.

function plot_line( line )

a = cell2mat(line);
b = {a.Centroid};

matx = {};
maty = {};

for i = 1:size(b,2)
    matx{end+1} = b{i}(1);
    maty{end+1} = b{i}(2);
end

plot ( cell2mat(matx), cell2mat(maty) );

end

Can you help me make this code nicer? It's not critical, as my code works fine and as the lines are short (<100 points) the performance is not an issue.

It is just that it would be really nice to know how this tiny function should be written in the proper way, without for loops and 3 calls of cell2mat.

In my example:

  • line is a <1xn cell>,
  • line{1} has a property 'Centroid' and
  • line{i}.Centroid(1) are the x coordinates and
  • line{i}.Centroid(2) are the y coordinates.

Actually, all I need is plotting line{i}.Centroid(1), line{i}.Centroid(2) for i = 1:size(line,2), but I don't know how.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天涯沦落人 2024-12-22 02:04:41

您可以使用 CAT:

tmp = cat(1,line{:});

coordinates = cat(1,tmp.Centroid);

plot(coordinates(:,1),coordinates(:,2))

编辑

如果你想保持它非常短(即甚至短于 @Amro的解决方案您可以使用CELLFUN 像这样以获得一句俏皮话:

plot(cellfun(@(x)x.Centroid(1),line),cellfun(@(x)x.Centroid(2),line))

Instead of creating a cell array b, you can create a numerical array directly, by catenating using CAT:

tmp = cat(1,line{:});

coordinates = cat(1,tmp.Centroid);

plot(coordinates(:,1),coordinates(:,2))

EDIT

If you want to keep it really short (i.e. even shorter than @Amro's solution you can use CELLFUN like this in order get a one-liner:

plot(cellfun(@(x)x.Centroid(1),line),cellfun(@(x)x.Centroid(2),line))
硬不硬你别怂 2024-12-22 02:04:41

示例:

line = repmat({struct('Centroid',[1 2])},1,5);   %# similar to the data you have

%# extract x/y coordinates
x = cellfun(@(s)s.Centroid(1),line)
y = cellfun(@(s)s.Centroid(2),line)

%# plot
plot(x,y)

您也可以这样做:

xy = cell2mat(cellfun(@(s)s.Centroid, line, 'UniformOutput',false)');
plot(xy(:,1),xy(:,2))

Example:

line = repmat({struct('Centroid',[1 2])},1,5);   %# similar to the data you have

%# extract x/y coordinates
x = cellfun(@(s)s.Centroid(1),line)
y = cellfun(@(s)s.Centroid(2),line)

%# plot
plot(x,y)

You could also do it as:

xy = cell2mat(cellfun(@(s)s.Centroid, line, 'UniformOutput',false)');
plot(xy(:,1),xy(:,2))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文