Making matrix from coordinates

Respuestas (1)

Ameer Hamza
Ameer Hamza el 11 de Oct. de 2020
Editada: Ameer Hamza el 11 de Oct. de 2020
See meshgrid()
x; % vector of x-values
y; % vector of y-values
[X, Y] = meshgrid(x, y)

5 comentarios

Ishani Mukherjee
Ishani Mukherjee el 11 de Oct. de 2020
Editada: Ishani Mukherjee el 11 de Oct. de 2020
Thanks for the prompt reply!
This makes 2 separate matrices.. Is there any way I can make a single matrix that has both x and y coordinates? Each value in the matrix would have a x coordinate and a y coordinate.
You can create a 3D matrix or a cell aray.
3D matrix:
x = [1,2,3];
y = [2,3,4];
[X, Y] = meshgrid(x, y);
XY = cat(3, X, Y);
Access values like this
>> XY(1,1,:)
ans(:,:,1) =
1
ans(:,:,2) =
2
However this will return a 3D vector with two elements. To get a normal 2D vector
>> squeeze(XY(1,1,:))
ans =
1
2
Cell array:
x = [1,2,3];
y = [2,3,4];
[X, Y] = meshgrid(x, y);
XY = arrayfun(@(x, y) {[x y]}, X, Y);
and access elements like this
>> XY{1,1}
ans =
1 2
Ishani Mukherjee
Ishani Mukherjee el 11 de Oct. de 2020
Thank you so much. Is there any way I can plot the matrix?
Something like this
x = [1,2,3];
y = [2,3,4];
[X, Y] = meshgrid(x, y);
plot(X(:), Y(:), '+')
Ishani Mukherjee
Ishani Mukherjee el 11 de Oct. de 2020
Thanks!!

Iniciar sesión para comentar.

Etiquetas

Preguntada:

el 11 de Oct. de 2020

Comentada:

el 11 de Oct. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by