MATLAB: Randomly changing a value of a matrix

matrix manipulation

Hello,
I've made a 5×5 matrix of zeros,
I want to randomly change one of the elements to a 1
How would i go about this?
Many thanks

Best Answer

M = zeros(5,5);
M(randi(25)) = 1;
This algorithm takes advantage of the power of linear indexing.