1

对于以下内容:

    x = [0.5 1.5 2.5 3.5 4.5];

    for k = 1:1:5
      plot(x(k),x','b^','linewidth', 2)
      hold on
    end

如同:

[x,y] = meshgrid(0.5:1:4.5);

我如何索引每个点(蓝色三角形)坐标?

在此处输入图像描述

结果应该是这样的:

point1  = [x(1),x(1)]; % [0.5,0.5]
point2  = [x(1),x(2)]; % [0.5,1.5]
point3  = [x(1),x(3)]; % [0.5,2.5]
point4  = [x(1),x(4)]; % [0.5,3.5]
point5  = [x(1),x(5)]; % [0.5,4.5]
point6  = [x(2),x(1)]; % [1.5,0.5]
...
point25  = [x(5),x(5)];% [4.5,4.5]

我必须做错事,否则 matlab 程序今天不让我索引这些。

[~,idx] = length(point(:));
idxpoint = ind2sub(size(point),idx);

请写一个工作示例。

先感谢您。

4

3 回答 3

2

你几乎拥有它。您可以meshgrid为此使用:

x = linspace(0.5, 4.5, 5);
y = linspace(0.5, 4.5, 5);
[Y, X] = meshgrid(x, y);

points = [X(:) Y(:)];

此方法的优点是您可以linspace对 x 和 y 坐标使用不同的坐标。

现在每一行points商店 x 和 y 坐标一个点:

points(1,:)
ans =

0.5000
0.5000

points(25,:)
ans =

4.5000
4.5000
于 2012-11-27T19:17:20.910 回答
1

下面的呢?

[x y] = meshgrid(.5:1:4.5);
points = [reshape(x,1,[])',reshape(y,1,[])']


points =

0.5000    0.5000
0.5000    1.5000
0.5000    2.5000
0.5000    3.5000
0.5000    4.5000
1.5000    0.5000
1.5000    1.5000
1.5000    2.5000
1.5000    3.5000
1.5000    4.5000
2.5000    0.5000
2.5000    1.5000
2.5000    2.5000
2.5000    3.5000
2.5000    4.5000
3.5000    0.5000
3.5000    1.5000
3.5000    2.5000
3.5000    3.5000
3.5000    4.5000
4.5000    0.5000
4.5000    1.5000
4.5000    2.5000
4.5000    3.5000
4.5000    4.5000
于 2012-11-27T19:16:34.987 回答
1

您可以将所有点堆叠成一个 N×2 矩阵,每一行代表一个点”

close all
x = [0.5 1.5 2.5 3.5 4.5];
n = length(x);
X = [];

for k = 1:1:5
    plot(x(k),x','b^','linewidth', 2)
    X = [X; repmat(x(k),n,1) x'];
    hold on
end

% replot on new figure
figure, hold on
plot(X(:,1),X(:,2),'b^','linewidth',2)

% Each row of X is one of your points, i.e.
% Point number 5:
X(5,:)
于 2012-11-27T19:12:52.857 回答