MATLAB Answers

function rand with variables

3 views (last 30 days)
Talmor Shlomovich
Talmor Shlomovich on 13 Nov 2019
Answered: Fangjun Jiang on 13 Nov 2019
hi,
i have 3 variables. for example:
a = 1
b = 2
c = 3
and i want to make a vector with 10 numbers only from the variables (a,b,c)
like "rand" but instead of random numbers - from the variable

  0 Comments

Sign in to comment.

Answers (2)

M
M on 13 Nov 2019
You can use something like this, adapt it to your case:
a = 1;
b = 2;
c = 3;
A = [a b c];
nb = 10;
vec = zeros(1,nb);
for i = 1 : nb
idx = randi([1 numel(A)]);
vec(i) = A(idx);
end

  0 Comments

Sign in to comment.


Fangjun Jiang
Fangjun Jiang on 13 Nov 2019
a=10;b=20;c=30;
mat=[a,b,c];
index=randi([1 3],1,10);
out=mat(index)

  0 Comments

Sign in to comment.