Function requires number as input; I want to enter a variable though!

Hi everyone! So, I am currently working on a code that uses the rand function. I have a variable set to a particular value, and the value changes based on the user's input to the function. I want to use this variable in the rand function. However, when I run the code, an error comes back stating that I can only enter numbers into the rand function, not variables. Any idea how I can get around this?
Thank you in advance!

Answers (1)

% rand column vectors
col = 3 ;
rand(col,1)
% rand row vectors
row = 4 ;
rand(1,row)
% matrix of rand
rand(row,col)
rand needs only number as input. If you still face problem, show the code how you have called it.

6 Comments

Okay, I tried it and I got the same result. So here is some example code. The resulting error is: error using rand, size inputs must be integers. The problem is that I want to enter variables into the rand function. Any insight? Sorry if I wasn't clear enough before.
A = 522;
B = 0;
col = 1;
amount = .34*(A*B);
ry = (A - B).*rand(amount,col) + 0;
I suggest you read the documentation of rand. The inputs, including the first one, define the size of the matrix returned by rand. Obviously a size has to be integer, and that's exactly what the error message is telling you.
I have no idea what you're trying to obtain with that amount variable. If it's supposed the be the upper bound of rand, then once again the documentation shows you exactly how to do that.
The point is that I have a function with one input. The variable, amount, is calculated by multiplication of two values: the user input and total. Therefore, I will need to input a variable into the rand function because the value will change based on the user's input. So, is it possible to somehow enter the variable into the rand function instead of a static number? I do not want to include my actual code because it would just be too confusing. Below you can find what I want to do in a nutshell.
function A = catalyst_dispersion(b)
total = 1
amount = b*total;
random = rand(amount,1);
If amount is fraction, then rand throws error. You may convert amount to an integer using either fix or round. Read about fix,round,ceil,floor.
Like this:
function A = catalyst_dispersion(b)
total = 1
amount = round(b*total);
random = rand(amount, 1);
Thank you KSSV and Image Analyst, this is exactly what I was looking for!! I appreciate the help!
Best, Sean

Sign in to comment.

Categories

Asked:

on 13 Jan 2017

Commented:

on 15 Jan 2017

Community Treasure Hunt

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

Start Hunting!