How can I save values in a 2d matrix corresponding to coordinates?

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?
For example, if I had
values = [1, 5, 22, 13, 8] x = [2, 4, 3, 3, 1] Y = [1, 5, 4, 3, 5]
Where a value of 1 is at coordinates (2,1), value of 5 is at (4,5), etc.
So the result would be something like
0 1 0 0 0
0 0 0 0 0
0 0 13 0 0
0 0 22 0 0
8 0 0 5 0

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2014년 11월 10일
accumarray([y(:),x(:)],values(:))

댓글 수: 3

Bob
Bob 2014년 11월 10일
편집: Bob 2014년 11월 10일
Sorry, I oversimplified my example. The X and Y coordinates contain some decimal values.
accumarray([Y(:),X(:)],values(:));
gives me the error
Error using accumarray
First input SUBS must contain positive integer subscripts.
I tried switching from kilometers to meters to get rid of the decimals, but got an out of memory error doing that.
To get rid of the decimals, use the ceil function.
Bob
Bob 2014년 11월 10일
편집: Bob 2014년 11월 10일
Thanks for your response. I think I may just need to repost with a more complex description of the problem. I actually will need those decimals to perform further calculations after the values are stored in the matrix, so I can't round them off.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Interpolation에 대해 자세히 알아보기

제품

질문:

Bob
2014년 11월 10일

편집:

Bob
2014년 11월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by