Random no matrix for 1 & -1

조회 수: 4 (최근 30일)
Offroad Jeep
Offroad Jeep 2015년 9월 4일
답변: James Tursa 2015년 9월 4일
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일
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일
Another way:
fm_array = 1 - 2*(rand(nrows) < 0.5);

카테고리

Help CenterFile Exchange에서 Random Number Generation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by