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

 採用された回答

Guillaume
Guillaume 2015 年 9 月 4 日

0 投票

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

1 件のコメント

Offroad Jeep
Offroad Jeep 2015 年 9 月 4 日
Thanks..... will you like to work with me in magnetism.......

サインインしてコメントする。

その他の回答 (1 件)

James Tursa
James Tursa 2015 年 9 月 4 日

0 投票

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

カテゴリ

ヘルプ センター および File ExchangeRandom Number Generation についてさらに検索

質問済み:

2015 年 9 月 4 日

回答済み:

2015 年 9 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by