Random no matrix for 1 & -1

1 view (last 30 days)
Offroad Jeep
Offroad Jeep on 4 Sep 2015
Answered: James Tursa on 4 Sep 2015
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

Accepted Answer

Guillaume
Guillaume on 4 Sep 2015
nrowcol = 5;
fm_array = rand(nrowcol);
fm_array(fm_array < 0.5) = -1;
fm_array(fm_array >= 0.5) = 1;
  1 Comment
Offroad Jeep
Offroad Jeep on 4 Sep 2015
Thanks..... will you like to work with me in magnetism.......

Sign in to comment.

More Answers (1)

James Tursa
James Tursa on 4 Sep 2015
Another way:
fm_array = 1 - 2*(rand(nrows) < 0.5);

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!