Randomly changing a value of a matrix

1 次查看(过去 30 天)
Hello,
I've made a 5x5 matrix of zeros,
I want to randomly change one of the elements to a 1
How would i go about this?
Many thanks

采纳的回答

the cyclist
the cyclist 2019-11-21
编辑:the cyclist 2019-11-21
M = zeros(5,5);
M(randi(25)) = 1;
This algorithm takes advantage of the power of linear indexing.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by