How to replace values of a vector based on the values of another vector?

3 views (last 30 days)
James on 11 Mar 2021
Commented: James on 15 Mar 2021
I have two vectors that together create a cartesian grid, such as this.
xmin = 0;
xmax = 500;
ymin = -200;
ymax = 200;
cell_size = 5;
x_cell(1) = xmin;
y_cell(1) = ymin;
for i = 1:2000
x_cell(i + 1) = xmin + cell_size * i;
if x_cell(i + 1) >= xmax
no_of_x_cells = i;
for i = 1:2000
y_cell(i + 1) = ymin + cell_size * i;
if y_cell(i + 1) >= ymax
no_of_y_cells = i;
r_vector = zeros(no_of_x_cells, no_of_y_cells);
I have another vector (I will call it "A" here) that includes x coordinates, y coordinates and arbitrary values (r). (I have attached the csv file that contains the data.)
Now, I want to map each arbitrary value (r) to the respective cell in my grid. So, for example, arbitrary value of the second element of A (which is 64.8436) will be included in (1,1) position of r_vector. And the arbitrary value of the 25th element of A (which is 5.7553) will be included in (18,1) position of r_vector. (Because x coordinate of A is 85.4922 and that belongs in 85-90 range of the x-grid, and y coordinate is -1.6285, which belongs in -5~0 range in y-grid. And so on.
Values that are not assigned will remain zero in r_vector, and if there are two values within the same cell, only the maximum value should be inserted.
I'm sorry if this is not descriptive enough or if it is too much to ask here. I've been struggling with this for a while now but I really cannot wrap my head around a possible approach. Any help or advice is appreciated. Thank you in advance!
(I'm also not sure about which tags to include here, so apologies)

Accepted Answer

Steven Lord
Steven Lord on 11 Mar 2021
Take a look at the histcounts2 function. From your description what I think what you want are the 4th and 5th outputs.
James on 15 Mar 2021
The latter. Basically the y axis has to be reversed. I didn't use the part:
ax.YDir = 'reverse';
But I was hoping that the bins will correspond to the bottom part - which is the negative side - automatically.

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!

Translated by