Assigning X-Y Coordinates to 2-D Matrix

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

Risposte (1)

Matt J
Matt J il 29 Mar 2022
Modificato: Matt J il 29 Mar 2022
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 Commenti

Torsten
Torsten il 29 Mar 2022
Modificato: Torsten il 29 Mar 2022
The OP wants to have
A(i,j) = [x(i),y(j)]
We had a long discussion that this is not possible for A being a usual numeric matrix.
Please convince him ...
If the desires is to have something like that, then one way would be.
[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
tinkyminky93
tinkyminky93 il 30 Mar 2022
Modificato: tinkyminky93 il 30 Mar 2022
I just want to see that A(1,1) = [0 0]
A(1,2) = [100 0] --> A(1,2) = [x_coordinates y_coordinates]
Matt J
Matt J il 30 Mar 2022
Modificato: Matt J il 30 Mar 2022
Isn't that what I've given you in my last comment?
Also, what is the intended benefit? You can can see from my original answer that you do not need it for plotting.
tinkyminky93
tinkyminky93 il 30 Mar 2022
Modificato: tinkyminky93 il 30 Mar 2022
It seems like it is 3-D array and it does not created with for loop. As I mentioned in the question, I am trying to assign the X and Y values by for loop. I did not get the permute and cat part of your code also. I am not familiar with them.
Think that the x values are
0 100 200
0 100 200
0 100 200
and the y values are
500 500 500
250 250 250
0 0 0
Put them in one matrix so that they become
Ax(3,1) = [0]
Ay(3,1) = [500]
Matt J
Matt J il 30 Mar 2022
Modificato: Matt J il 30 Mar 2022
it does not created with for loop
Yes, it ihas already been explained to you in a previous thread that meshgrid or ndgrid are better for this than a for-loop. Also, you already have a for-loop that creates the x,y data (as separate matrices), so you clearly do not need help with that part of it if you really insist on using a for loop.
It seems like it is 3-D array
Yes. It has already been explained to you in a previous thread that you cannot have a 2D matrix consisting of non-scalar elements. So, I'm not sure why you would like to repeat that discussion.
I did not get the permute and cat part of your code also. I am not familiar with them.
Perhaps you should read the documentation about them.
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.
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

Accedi per commentare.

Prodotti

Release

R2021b

Richiesto:

il 29 Mar 2022

Modificato:

il 31 Mar 2022

Community Treasure Hunt

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

Start Hunting!

Translated by