I am new to Matlab and I am trying to create programmatically a square Matrix which may have some random stochastic columns but I can't come up with a working solution. By stochastic column I mean the sum of the column elements which are positive should be equal to 1 (the column non-zero elements should be the same and their sum equal to 1). The position of the non-zero elements is not important in the matrix. PS: some columns could be all-zero elements and some could be with only one non-zero element, in this case 1.
I need your guidance or an example of working code on this guys. Thanks in advance.
Here is an example:
A=
0.2500 0.5000 0 0 0 0 0
0.2500 0.5000 0.3333 0 0 0 0
0 0 0.3333 0.2500 0 0 0.3333
0.2500 0 0 0.2500 0 0.5000 0
0.2500 0 0 0.2500 0 0 0
0 0 0 0.2500 0 0.5000 0.3333
0 0 0.3333 0 0 0 0.3333
% here is my code but it's not doing the work yet
n = 5;
A = zeros(n, 5);
i = 0;
for i = 1:n
if rand < 0.5
i = i + 1;
A(i, :) = rand(1, 5);
end
end
A = A(1:i, :)