Making matrix from coordinates
Show older comments
I have 460 x and y coordinates. How do I make a matrix ? Thanks in advance.
Answers (1)
Ameer Hamza
on 11 Oct 2020
Edited: Ameer Hamza
on 11 Oct 2020
See meshgrid()
x; % vector of x-values
y; % vector of y-values
[X, Y] = meshgrid(x, y)
5 Comments
Ishani Mukherjee
on 11 Oct 2020
Edited: Ishani Mukherjee
on 11 Oct 2020
Ameer Hamza
on 11 Oct 2020
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
on 11 Oct 2020
Ameer Hamza
on 11 Oct 2020
Something like this
x = [1,2,3];
y = [2,3,4];
[X, Y] = meshgrid(x, y);
plot(X(:), Y(:), '+')
Ishani Mukherjee
on 11 Oct 2020
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!