2012-11-27 27 views
1

爲以下幾點:Matlab的:指數5×5網格點

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

請寫一個工作示例。

預先感謝您。

+1

很抱歉,但我不明白你的問題。一個完全盲目的猜測,如果沒有完全理解你的問題,我會說你應該看看重塑。 – 2012-11-27 18:58:26

回答

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座標。

現在的points商店x和y的每一行的座標一點:

points(1,:) 
ans = 

0.5000 
0.5000 

points(25,:) 
ans = 

4.5000 
4.5000 
+0

一如既往的教育,做好@angainor。 – professor

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,:) 
+0

謝謝你的方法。 – professor

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 
+0

謝謝我的觀點。 – professor

+0

@pr教授,不客氣 – Acorbe