Generate random matrix with specific values [closed] - matlab

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am new to MATLAB and I want to create a random n*n matrix containing just -1 OR 1 as values.
any help ?

I would use randi
% Generate random array of 0s and 1s, *2 and -1 to give random values -1 or +1
m = randi([0,1], n)*2-1
See also: introductory docs on random integers.

I typically use randi to generate the indexes of the numbers I am interested in. E.g. in your case you are interested in the numbers
a= [-1,1];
Thus we use
b = randi(length(a),2,2); %Generate matrix of size 2x2
to generate a random set of indexes. Finally we simply convert the indexes to the numbers of interest.
c = a(b); %Now a 2x2 matrix of -1, 1 numbers

A=rand(n);
thres=rand(1); % or whatever percentage
A=A>thres; % 1 and 0
A(A==0)=-1; % makes 0 -1

Related

Random Number Generation for Continuously Decrementing Range [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How can I create a matrix of a size 50 x 50 in MATLAB and each column vector of matrix i.e., 50 x 1 size must have random numbers within a given range and then range for the next column vectors must decrement by k until last column vector is reached?
A solution may be to generate two matrices:
A 50x50 matrix containing random values between 0 and 1
A 50x50 matrix containing the upper limits for each cell
The elementwise multiplication of both matrices should give you the desired matrix.

how to randomly select an element from an array having the least probability [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
how to randomly select an element from an array having the least probability in matlab
example :
A = [ 1,2,3,4,5,6]
P = [ 0.01,0.2,0.25.0.2,0.25,0.09] % probability %
Find all indices of the minimum probabilities. Then generate a random interger depending on the number of equally minimum probabilities. Then use matrix indexing to extract a random index of minimum probability and then its corresponding element.
[~, ind] = mink(P,2); %All indices of minimum elements
Result = A(inds(randi(numel(inds)))); %Random minimum element

Random matlab matrix with at least .4 and at most .6 ones per column [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm creating a matrix in matlab of size n that contains only ones and zeros. The easiest way to do that is round(rand(m,n)) for a matrix of size mxn, but it creates in some cases rows that have all zeros or all ones. I want to put a lower and upper bound in the number of ones that each row has. Is there an easy way to do that?
Thanks
This is just for one column, but can easily be extended to a matrix:
v = zeros(m,1); % column
Fill the beginning of the column with at least 40% and at most 60% ones:
v(1: floor((0.4+(0.6-0.4)*rand())*(m+1))) = 1;
Shuffle the column:
v = v(randperm(numel(v)));

Octave beginner help. Creating a function that takes a component matrix as a parameter and returns a matrix? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to create a function that takes a component matrix as a parameter and returns a matrix?
Apparently this function should normalise my data?
There are other instructions along with this step in my project such as:
Take the matrix and calculate the mean value along a certain column.
Calculate the difference between the measurement and this mean.
Subtract this difference from each measurement.
Return corrected matrix to the script.
Place corrected matrix in a variable within the script.
(I don't know if this is what the function is supposed to do or anything I'm completely lost and any help would be appreciated thanks!)
This is probably homework but I'll help you get started.
To create a function which takes a matrix and return a matrix:
function m_out = my_function(m_in)
%insert calculations here
end
To find the 2-norm of a matrix (which is the largest singular value):
the_norm = norm(my_matrix); % returns a scalar, 2-norm of matrix
To find the mean of a vector:
the_mean = mean(my_vector); % returns a scalar, mean of the vector
To access a specific column of a matrix:
my_col = my_matrix(:, col_number); % my_col is a vector
To access a specific row of a matrix:
my_row = my_matrix(row_num, :); % my_row is a vector
To subtract a scalar (single number) from a matrix:
new_matrix = old_matrix - single_number; % returns a matrix
To store a matrix into a variable (example):
my_matrix = [1,2,3;4,5,6;7,8,9];
Give it a try creating a function which puts this all together.

Neural network that learns patterns in very large matrices [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
Suppose we have some large matrices that want to classify.
If matrices are MN, is it OK to have a neural network with MN neurons that is, for each element a neuron ? Is there any better idea? Actually, in my case, I have 50 matrices with 200 rows and 200 columns [ 200*200 ].
{The elements of input matrices are 0 and 1 }
Thank you in advance.
Find a suitable value for the number of hidden nodes, H, by trial and error.
Create numH*Ntrials plus An outer loop H = Hmin:dH:Hmax over number of hidden nodes and an inner loop i = 1:
Ntrials over number of random trn/val/tst data divisions and random weight initialization trials for each value of H.