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

조회 수: 13 (최근 30일)
Jake
Jake 2021년 3월 11일
댓글: Jake 2021년 3월 15일
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)

채택된 답변

Steven Lord
Steven Lord 2021년 3월 11일
Take a look at the histcounts2 function. From your description what I think what you want are the 4th and 5th outputs.
  댓글 수: 5
Steven Lord
Steven Lord 2021년 3월 14일
Your Y axis has the smallest values at the top and your y_cell variable is in increasing order. So the first row of bins (in the matrix) corresponds to the top of your plot. Is that what you expected? Or were you expecting the first row of bins to correspond to the bottom of your plot?
Jake
Jake 2021년 3월 15일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by