Unequal array lengths of vectors

10 visualizaciones (últimos 30 días)
Lamba Opa
Lamba Opa el 18 de En. de 2018
Editada: Lamba Opa el 19 de En. de 2018
I have two vectors, x = 5,6,4,8,9 and y = 3, 2, 4
How would I create a 5x5 matrix or a 7x8 matrix using X and Y?
  6 comentarios
Steven Lord
Steven Lord el 18 de En. de 2018
There are many ways to create a 5-by-5 matrix using x and y. Here are four examples.
x = [5, 6, 4, 8, 9];
y = [3, 2, 4];
v = [x, y];
% Ignore the vectors entirely
A = eye(5);
% Only use one of the arrays
B = repmat(x, 5, 1);
% Random selection of elements from the combined vector
C = v(randi(length(v), [5 5]));
% Keep replicating the elements in order until you have enough
n = ceil(25/length(v));
v2 = repmat(v, 1, n);
D = reshape(v2(1:25), 5, 5);
If you describe in more detail what you want the result to look like (A, B, C, D, or something else entirely -- and in that last case, please be specific) we can suggest ways to achieve that result.
Lamba Opa
Lamba Opa el 18 de En. de 2018
The matrix I want to output for a 7 by 8, but it could be any matrix that uses those two vectors to create the matrix:
5 6 7 8 9 3 2 4
5 6 7 8 9 3 2 4
5 6 7 8 9 3 2 4
5 6 7 8 9 3 2 4
5 6 7 8 9 3 2 4
5 6 7 8 9 3 2 4
5 6 7 8 9 3 2 4

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 19 de En. de 2018
repmat([x,y], 7, 1)

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by