how can i convert 1xn to mxn matrix ?

4 views (last 30 days)
Gn Gnk
Gn Gnk on 20 Oct 2019
Answered: Walter Roberson on 20 Oct 2019
Hello i have a variable x[k] k=1,...100
that takes some random numbers as an input, so i have a 1x101 matrix.
How can i create a matrix 20x101 .I tried the repmat function that works but
all of the rows are the same number because this function copies everything
for the first row . How can i actually create 20x101 matrix and still have random numbers
to each row?
Thank you in advance.

Answers (2)

David Hill
David Hill on 20 Oct 2019
Look at rand or randi functions. Depending on what range you want your randon numbers to be in, either function could be used.
x = randi(100,20,101);
Will give you a 20x101 matrix of randon numbers between 1 and 100.

Walter Roberson
Walter Roberson on 20 Oct 2019
Use persistent variables in your function. The first 19 times the function is called, just store the 100 values passed in. The 20th time the function is called, store the 100 values and then process the 20 x 100 values to create your 20 x 101 matrix of output and return that.

Community Treasure Hunt

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

Start Hunting!