Save values in a 2d matrix corresponding to coordinates

4 visualizaciones (últimos 30 días)
Elise
Elise el 8 de Jul. de 2015
Editada: Stephen23 el 8 de Jul. de 2015
I have a set of values that correspond to certain coordinates on a grid. How can I create a matrix that stores these values in the corresponding location, with blank values for all other points on the matrix?
I have three sets of data x,y and values
simplified
x = [1, 5, 6, 7]
y = [1, 4, 6, 4]
values = [5, 3, 4, 6]
where value of 5 is located at point (1,1)
I have earlier asked this question and gotten the
accumarray ([x(:),y(:)],values(:))
which works fine with the simplified values. The problem is that my x and y coordinates are decimals, and I wish to keep them this way. Is there a way around this problem in accumarray or some other example of how this can be done?

Respuesta aceptada

Stephen23
Stephen23 el 8 de Jul. de 2015
Editada: Stephen23 el 8 de Jul. de 2015
You need a way to convert decimal values to a list of indices. One easy way to do this is to use unique, or even better to use uniquetol (more recent versions only):
tol = 0.1; % pick appropriate tolerance
values = [5, 3, 4, 6];
x = [1.1, 5.2, 6.3, 7.4];
y = [1.1, 4.2, 6.3, 4.2];
[xv,~,xp] = unique(round(x/tol));
[yv,~,yp] = unique(round(y/tol));
accumarray([xp(:),yp(:)],values(:))
displays this in the command window:
ans =
5 0 0
0 3 0
0 0 4
0 6 0
>> xv*tol % rows correspond to these values:
xv =
1.1000 5.2000 6.3000 7.4000
>> yv*tol % columns correspond to these values:
yv =
1.1000 4.2000 6.3000
  3 comentarios
Stephen23
Stephen23 el 8 de Jul. de 2015
Editada: Stephen23 el 8 de Jul. de 2015
Can you give some sample values please. It is not clear what you require, as matrices do not have non-integer indices *. If you have specific requirements, then these need to be clearly explained. It seems that you want the final matrix to be a "grid" that includes columns and rows for values that do not occur in the data. If this is the case, then maybe you want something like this:
tol = 0.5; % pick appropriate tolerance
values = [5, 3, 4, 6];
x = [1, 2.5, 2.5, 3];
y = [1, 1.5, 2.5, 1];
xp = round(x/tol);
yp = round(y/tol);
accumarray([xp(:)-min(xp)+1,yp(:)-min(yp)+1], values(:))
Which displays this in the command window:
ans =
5 0 0 0
0 0 0 0
0 0 0 0
0 3 0 4
6 0 0 0
>> tol*(min(xp):max(xp)) % row values
ans =
1.0000 1.5000 2.0000 2.5000 3.0000
>> tol*(min(yp):max(yp)) % column values
ans =
1.0000 1.5000 2.0000 2.5000
Of course it is easy to adjust the grid, but you have to define it first!
* not in standard mathematics anyway
Elise
Elise el 8 de Jul. de 2015
Thank you! This was great help!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by