how to put an array into a matrix

1 visualización (últimos 30 días)
Chien-Chia Huang
Chien-Chia Huang el 10 de Abr. de 2011
Hi there!
the function "triu(X,1)" returns the upper trangular part of the matrix X. I now have an array of size 1-by-(0.5*n*(n-1)), exactly the number of elements in the upper triangular. How can I, without using any loop, put this array into a n-by-n matrix so that this matrix is what triu(X,1) returns?
Thanks. Lianovich

Respuesta aceptada

Walter Roberson
Walter Roberson el 10 de Abr. de 2011
n = floor(sqrt(2*length(V)));
M = zeros(n);
M(logical(triu(ones(n),1))) = V;
  3 comentarios
Chien-Chia Huang
Chien-Chia Huang el 10 de Abr. de 2011
Thanks Walter.
But when I tried to put [1 2 3] into a 3-by-3 square matrix, the error message occured:
??? In an assignment A(I) = B, the number of elements in B and
I must be the same.
Error in ==> arraytomatrix at 9
M(logical(triu(ones(n),1))) = V;
Guess I must have mistaken what you meant.
What should the V be?
Chien-Chia Huang
Chien-Chia Huang el 10 de Abr. de 2011
Yup, Matt is right.
Thanks, Matt.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements 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