How to replace values of a vector based on the values of another vector?
    24 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Jake
 el 11 de Mzo. de 2021
  
    
    
    
    
    Comentada: Jake
 el 15 de Mzo. de 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;
        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 comentarios
Respuesta aceptada
  Steven Lord
    
      
 el 11 de Mzo. de 2021
        Take a look at the histcounts2 function. From your description what I think what you want are the 4th and 5th outputs.
5 comentarios
  Steven Lord
    
      
 el 14 de Mzo. de 2021
				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?
Más respuestas (0)
Ver también
Categorías
				Más información sobre Matrices and Arrays en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



