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

3 views (last 30 days)

Show older comments

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;

break

end

end

for i = 1:2000

y_cell(i + 1) = ymin + cell_size * i;

if y_cell(i + 1) >= ymax

no_of_y_cells = i;

break

end

end

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)

##### 0 Comments

### Accepted Answer

Steven Lord
on 11 Mar 2021

### More Answers (0)

### See Also

### Community Treasure Hunt

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

Start Hunting!