I want to generate a random matrix for which if element is < 0.5 it is equal to -1 and if >=0.5 its equal to 1 . kindly correct the code.......... I have attached....... Thanks
clc
clear all
format compact
nrows= 5
fm_array = rand(nrows)
if fm_array >= 0.5
fm_array == 1
else
fm_array == -1
fm_array
end

 Respuesta aceptada

Guillaume
Guillaume el 4 de Sept. de 2015

0 votos

nrowcol = 5;
fm_array = rand(nrowcol);
fm_array(fm_array < 0.5) = -1;
fm_array(fm_array >= 0.5) = 1;

1 comentario

Offroad Jeep
Offroad Jeep el 4 de Sept. de 2015
Thanks..... will you like to work with me in magnetism.......

Iniciar sesión para comentar.

Más respuestas (1)

James Tursa
James Tursa el 4 de Sept. de 2015

0 votos

Another way:
fm_array = 1 - 2*(rand(nrows) < 0.5);

Categorías

Más información sobre Random Number Generation en Centro de ayuda y File Exchange.

Preguntada:

el 4 de Sept. de 2015

Respondida:

el 4 de Sept. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by