Create a random Matrix

I have a 8 column vector n*1. Can I create a Random matrix of n*8?
The function must take one value from each vector and create a matrix with 8 columns and n rows.
Please help me :-)

2 comentarios

Jan
Jan el 24 de Oct. de 2011
"I have a 8 column vector" sounds like a single vector. But "from each vector" might mean n vectors. Confusing.
Maurizio
Maurizio el 24 de Oct. de 2011
I have 8 different variable. Each variable is a column vector (nx1).
What I need is to have as OUT a random matrix with 8 column and n rows. The value on the first OUT Column must be only the value of the first IN column

Iniciar sesión para comentar.

 Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 24 de Oct. de 2011

1 voto

A1,A2,..,A8 - your vectors (nx1), let n = 5;
for j1 = 1:8, eval([sprintf('A%d',j1),'=randi(20,5,1)']); end
A = eval(['[',sprintf('A%d,',1:8),']'])
out=A(bsxfun(@plus,cell2mat(arrayfun(@(x)randperm(n)',(1:8),'un',0)),(0:7)*n))

3 comentarios

Maurizio
Maurizio el 24 de Oct. de 2011
Thanks andrei, the problem that the OUT columns value on each columns must be the same of the IN columns. For example the values that I have on the third OUT column must be one of the third IN column.
Jan
Jan el 31 de Oct. de 2011
@Maurizio: Accepting an answer means, that it solves your problem sufficiently.
Maurizio
Maurizio el 31 de Oct. de 2011
@JAN: I accept but because create a random Matrix but as you can see from my comment Is that there was some problem about OUT columns values

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 24 de Oct. de 2011

0 votos

rv(:,1) = randomvector1;
rv(:,2) = randomvector2;
rv(:,3) = randomvector3;
....
rv(:,4) = randomvector4;
rants = zeros(8, n);
for K = 1:n
rants(:,K) = A Random Selection of One Entry Per Column from rv
end
How to do "A Random Selection of One Entry Per Column from rv" is left to you to work on in more detail. I will hint that it involves using rand().

3 comentarios

Maurizio
Maurizio el 30 de Oct. de 2011
Can you help me what I need to write on the for loop please?
Maurizio
Maurizio el 30 de Oct. de 2011
Can you help me what I need to write on the for loop please?
Walter Roberson
Walter Roberson el 30 de Oct. de 2011
floor(1 + n * rand())
will give you a random row number.
If I say much more than I will be completing your assignment for you.

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by