Borrar filtros
Borrar filtros

Comma separated list generation

2 visualizaciones (últimos 30 días)
Jan
Jan el 22 de Jun. de 2011
Editada: Stephen23 el 22 de Feb. de 2024
How can you expand a comma separated list from a repetition? I am searching for the elegant way to do something like this:
Instead of
Y = blkdiag(X, X, X),
write Y = repblkdiag(X,3). The code I use now is:
function Y = repblkdiag(X, n)
Y = [];
for j = 1:n
Y = blkdiag(Y, X);
end
I expected that something like
Y = blkdiag(deal(repmat(X,3)))
would work. Thanks for your interest and contribution,
Jan

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 22 de Jun. de 2011
xc = repmat({X},1,3)
Y = blkdiag(xc{:})
  4 comentarios
Teja Muppirala
Teja Muppirala el 22 de Jun. de 2011
It is possible to do it in one line, but I think Andrei's solution is simpler.
Y = eval(['blkdiag( ' repmat('X,',1,3) '[])'])
Stephen23
Stephen23 el 22 de Feb. de 2024
Editada: Stephen23 el 22 de Feb. de 2024
Another neat approach:
C = {X};
Y = blkdiag(C{[1,1,1]})
One line (since R2019b):
Y = blkdiag(struct('x',repmat({X},1,3)).x)
... but I recommend using two lines.

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.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by