Convert x y coordinates and z data to Matrix

17 views (last 30 days)
I have three vectors
>> whos x y VAR1
Name Size Bytes Class Attributes
x 21242x1 679744 double
y 21242x1 679744 double
var1 21242x1 679744 double
where X and Y represent the location of the corresponding value of var1. What Im trying to do is to convert all this data to a matrix.
What I've done until now is:
a1 = 21242;
m = zeros(a1,a1).*NaN;
indexes = sub2ind([a1,a1],x,y);
m(indexes) = var1
Based on this question but I'm getting an error
Error using sub2ind (line 43)
Out of range subscript.
Which, according to another question about this error, is because I'm trying access elements which does not exist on the matrix.
Any help?
Dyuman Joshi
Dyuman Joshi on 15 Sep 2022
Indices should also be integers as well.

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 15 Sep 2022
It would help to have the actual vectors.
It may be possible to use the reshape function to create matrices from the vectors, if the vectors are from a gridded original matrix. The easiest way to determine the dimensions of the arrays is to use the unique function and look at the second output. The elements of the second output should be regularly-spaced, and that difference is one dimension to specify in the reshape call, with the other one left as the empty vector [].
Otherwise, use linspace, ndgrid and griddata to create appropriate matrices from scattered data.

Sign in to comment.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!