Matlab:5 x 5网格点的索引

时间:2012-11-27 18:51:38

标签: matlab indexing

对于以下内容:

    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);

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

enter image description here

结果应该是这样的:

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);

请写一个有效的例子。

提前谢谢。

3 个答案:

答案 0 :(得分:2)

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

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

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

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

现在points的每一行都存储x和y一个点:

points(1,:)
ans =

0.5000
0.5000

points(25,:)
ans =

4.5000
4.5000

答案 1 :(得分:1)

您可以将所有点堆叠成N-by-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,:)

答案 2 :(得分: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