필터 지우기
필터 지우기

Assigning X-Y Coordinates to 2-D Matrix

조회 수: 40 (최근 30일)
tinkyminky93
tinkyminky93 2022년 3월 29일
편집: Matt J 2022년 3월 31일
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

답변 (1개)

Matt J
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])
x = 3×3
0 100 200 0 100 200 0 100 200
y = 3×3
200 200 200 100 100 100 0 0 0
plot(x(:),y(:),'*'); axis padded
  댓글 수: 8
tinkyminky93
tinkyminky93 2022년 3월 31일
I want to reach them individually, so please write me a code block that gives me the element of this matrix and it also shows the coordinates. Plotting or writing [0:250:500] is the easy part and have no meaning for me. I just want to use the elements of that matrix. Hope it is clear sir.
Matt J
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)
ans = 1×2
100 200

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by