Indexing of 1D arrays using 2D mapping information

39 views (last 30 days)
I have 3 one-dimensional arrays, 2 of them contain XY coordinates while the third (called 'E') contains some other non spatial parameter (i.e. electric field intensity).
I would like to create a heatmap of the values of E, based on the corresponding XY positions. Each vector has the same numerosity (around 3000 points). However, the XY coordinates are not regularly spaced (this is because they are extracted from a moving probe, whch movement was then tracked).
So, what I did so far was:
1) create an empty matrix to fill, sized as the area covered by the probe
% determine field size
size_X = abs(max(xd)-min(xd));
size_Y = abs(max(yd)-min(yd));
% create blank map
map = zeros(size_Y,size_X);
2) try to fill the 2D array indexing the closest points to each of the "standardized" array slots using a loop
for j = 1:30 & row index
for i = 1: 20 % column index
[~, x_index]=min(abs(xd-i));
[~, y_index]=min(abs(yd-j));
map(j,i) = E(find the index of the E array corresponding to xd(i) and yd(j)???);
I thought to use something like sub2ind to find the correct index.. but the problem is that E is not a 2D map just a straight vector such X and Y. I also tried to map E on X and Y using the scatter function but somehow the result is not what I would like to have.
should I interpolate the values the spatial vectors and then use the params to transform E ? or something like that... ? I am not sure
thank you for any tip! :)

Accepted Answer

chicken vector
chicken vector on 5 Jul 2023
Edited: chicken vector on 5 Jul 2023
Have a look at this:
An adaption from griddata example:
% Create fake data (X,Y,E):
nPoints = 300;
x = -2.5 + 5*rand(nPoints,1);
y = -2.5 + 5*rand(nPoints,1);
v = x.*exp(-x.^2-y.^2);
% Interpolate the scattered data to obtain the regular grid:
[xq,yq] = meshgrid(min(x):.1:max(x), min(y):.1:max(y));
vq = griddata(x,y,v,xq,yq);
color = (v-min(v))/(max(v)-min(v));
xlim([min(x) max(x)])
ylim([min(y) max(y)])

More Answers (1)

Pranavkumar Mallela
Pranavkumar Mallela on 5 Jul 2023
Edited: Pranavkumar Mallela on 6 Jul 2023
As per my understanding, you want to make a heat map for a given set of (X,Y) coordinates.
In your code, you are iterating over the 'standardized slots' and trying to map the E value to the corresponding slot. Instead, if you iterate over the data points, you could map the E value based on the approx index of each data point. This can be done using the following formula:
newValue = newMin + ((newMax - newMin) / (oldMax - oldMin))*(value - oldMin)
% determine field size
size_X = abs(max(xd)-min(xd));
size_Y = abs(max(yd)-min(yd));
% create blank map
map = zeros(size_Y,size_X);
% Iterate over data points
for i = 1:length(xd)
x_index = x_mappedIndex(xd(i)); % use the formula above to define this function
y_index = y_mappedIndex(yd(i)); % use the formula above to define this function
map(x_index, y_index) = E(i);
This way, mapping the "E" value would be straightforward. I hope this helps! Thanks!


Find more on Matrices and Arrays 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!