Convert x y coordinates and z data to Matrix
Show older comments
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
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?
Thanks!
5 Comments
indexes = sub2ind([a1,a1],x,y)
What are the values in x and y?
By the error it seems that values in x and y exceed a1*a1
Also, you can use NaN directly
%example for 2x2
m = NaN(2,2)
user20912
on 15 Sep 2022
Dyuman Joshi
on 15 Sep 2022
Edited: Dyuman Joshi
on 15 Sep 2022
Yes, it could.
Negetives values are not accepted as indices in MATLAB (even 0 is not accepted for that matter)
i1=sub2ind([3 3],2,2)
i2=sub2ind([3 3],-2,2)
user20912
on 15 Sep 2022
Dyuman Joshi
on 15 Sep 2022
Indices should also be integers as well.
Accepted Answer
More Answers (1)
Robert Daly
on 11 Aug 2023
0 votes
I would use interpolation to convert to a regular grid.
scatteredInterpolant is the funtion to do this with your irregularly spaced data.
[X,Y] = meshgrid(linspace(min(x),max(x),500),linspace(min(y),max(y),500));% make 500x500 grid
f = scatteredInterpolant(x,y,var1); % set up the interpolater with your data
Grid_Var1 = f(X,Y); % Do the interpolation onto the grid set up above
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!


