Borrar filtros
Borrar filtros

Assigning Random Weights at each Loop Iteration

1 visualización (últimos 30 días)
A M
A M el 25 de En. de 2017
Comentada: Walter Roberson el 26 de En. de 2017
I am trying to assign Random Weights (between 0 and 1) in each loop iteration. Could you please guide that how to do the same in this chunk of code, such that W gets new values between 0 and 1 each time the loop repeats itself.
W = [.41 .99 .51 .32 .15 .45 .38 .32 .36 .29 .21];
DG = sparse([6 1 2 2 3 4 4 5 5 6 1],[2 6 3 5 4 1 6 3 4 3 5],W)

Respuestas (2)

Andrei Bobrov
Andrei Bobrov el 25 de En. de 2017
W = rand(1,11);
DG = sparse([6 1 2 2 3 4 4 5 5 6 1],[2 6 3 5 4 1 6 3 4 3 5],W)
  2 comentarios
Walter Roberson
Walter Roberson el 25 de En. de 2017
Your original matrix had 11 weights. Your indices now have length 12. You need W = rand(1,12) to match.
A M
A M el 26 de En. de 2017
Yes, i figured out. Thanks a lot for the help :-)

Iniciar sesión para comentar.


Walter Roberson
Walter Roberson el 25 de En. de 2017
Have you considered using sprand() ?
  2 comentarios
A M
A M el 26 de En. de 2017
How to use sprand() in this case? If i m not mistaken, sprand() returs a mxn matrix.
In above case, rand(1,12) is returning a vector. Isn't so?
Walter Roberson
Walter Roberson el 26 de En. de 2017
sprand returns an m x n sparse matrix filled to occupancy you specify. The locations are chosen at random.
Your call to sparse() is creating a 6 x 6 sparse matrix and using fixed positions to store the values. That is definitely something that needs to be done sometimes, but if it happens that the code you did not show us is picking out those indices at random then sprand() might be more convenient. Except, that is, that sprand() is filled by probability, not by fixed number of non-zero results.

Iniciar sesión para comentar.

Categorías

Más información sobre Sparse Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by