Creating a Matrix of random numbers
Show older comments
I'm trying to create a 20x20 matrix of values either -1 or 1, but randomly assigned. How can i do this? I've tried using the randi function but it returns the numbers as a range from -1 1 and so includes 0. any help would be appreciated
Accepted Answer
More Answers (4)
John D'Errico
on 21 Nov 2020
randi([0 1],20)*2 - 1
David Hill
on 21 Nov 2020
(-1).^randi(2,20);
Setsuna Yuuki.
on 21 Nov 2020
A long way
bits= randi([-1 1],20,20);
[r,c,~] = size(bits);
count = 1;
while count < (r*c)+1
bits = reshape(bits,[1 r*c]);
if(bits(count) == 0)
bits(count) = randi([-1 1],1);
count = 1;
else
count = count + 1;
end
end
bits = reshape(bits,[r c]);
venkata datta sai krishna
on 25 Nov 2022
0 votes
create a random integer 4*4 matrix A with rank equals 2(maximum only two columns are independent) and demonstrate above factorisation in matlab
Categories
Find more on Random Number Generation 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!