Assigning X-Y Coordinates to 2-D Matrix
40 ビュー (過去 30 日間)
古いコメントを表示
Hello, I am trying to create a matrix like shown below and plot them like shown too. It is a simple 3x3 matrix and it contains X-Y coordinates of a point. I am trying to create this matrix with "for" loop but I cannot assign 2 values (x and y) to an element.
I will index the elements of the matrix for further work so I need these elements individually with their x-y coordinates.
I tried the code below but I know it is not true. Also the plot has some problems. Can you help me? Thanks.
close all
clear all
clc
Z = zeros(3,3)
for i=1:1:3
for j = 1:1:3
x = [0:100:200];
y = [200:-100:0];
A(i,j) = [x(i)];
B(i,j) = [y(j)];
Z = [A B];
plot(Z, '*');
end
end
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/945414/image.png)
0 件のコメント
回答 (1 件)
Matt J
2022 年 3 月 29 日
編集済み: Matt J
2022 年 3 月 29 日
What I think you are trying to do is,
[x,y]=meshgrid([0,100,200],[200 100 0])
plot(x(:),y(:),'*'); axis padded
8 件のコメント
Matt J
2022 年 3 月 31 日
編集済み: Matt J
2022 年 3 月 31 日
I believe I have done that already. You have asked for a way to access an individual x,y pair with the syntax A(i,j). By now, you will know that that is impossible, however, I showed you earlier that you can do so with essentially the same syntax A(i,:,j),
[x,y]=meshgrid([0,100,200],[200 100 0]);
A=permute( cat(3,x,y) ,[1,3,2]);
A(1,:,2)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!