MATLAB: How to generate a random 2×2 matrix with values in the range −1 to 1 matrixrandom Hi, I'm new to matlab and trying to generate a random 2×2 matrix with values in the range -1 to 1. Thank you for your time. Best Answer This works:M = 2*rand(2)-1; Related SolutionsMATLAB: Creating a Matrix of random numbers possibleValues = [-1, 1];desiredSize = [5 6];A = possibleValues(randi(numel(possibleValues), desiredSize))A = 5×6 -1 1 1 1 1 -1 -1 -1 1 1 -1 -1 -1 -1 -1 1 -1 -1 1 1 -1 1 1 -1 -1 1 -1 1 1 -1 This assumes you want -1 and 1 to be equally likely. If you need an exact number of each possible value:elements = [repmat(-1, 1, 5), repmat(1, 1, 25)];order = randperm(numel(elements));shuffled = reshape(elements(order), desiredSize)shuffled = 5×6 1 -1 1 1 1 1 1 1 1 1 -1 1 1 1 -1 1 1 1 -1 1 1 1 1 1 -1 1 1 1 1 1 ●If you need the two numbers not to be equally likely (say 1 three times as likely as -1) there are ways to do this as well. MATLAB: Matrix[1,1,1,1;-1,-1,-1,-1;1,1,1,1] to vector[1,1,1,1,-1,-1,-1,-1,1,1,1,1] I believe you intended semicolons (;) instead of colons (:) in your matrix definition.To convert it to a vector, use the reshape function:Matrix = [1,1,1,1; -1,-1,-1,-1; 1,1,1,1];Vector = reshape(Matrix', 1, [])Vector = 1 1 1 1 -1 -1 -1 -1 1 1 1 1EDIT — Added output of ‘Vector’. Related QuestionRandom number between 0 and 1How to convert a 64 bit, two’s complement value(binary vector)[ 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1] into a signed integerHow can I create a matrix of zeros and ones with dimension 2^n*nMATLAB 2×2 maximum fillCreation of matrix (72,1) by defined elements
Best Answer