MATLAB: How to assign a value to an especific coordinate in a matrix

iteratematrixvectors

I have two vectors x and y and I want to assign to the coordinate (x(1),y(1)) of a matrix a number, and then the same number to the coordinate (x(2),y(2)) and so on until I get to the coordinate (x(n),y(n)).
How can I do this without iterating?

Best Answer

YourMatrix(sub2ind(size(YourMatrix), x, y)) = TheNumber;
In the case where YourMatrix does not exist yet you can also use
YourMatrix = sparse(x, y, TheNumber);