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

 Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 22 Jun. 2011

0 Stimmen

xc = repmat({X},1,3)
Y = blkdiag(xc{:})

4 Kommentare

Jan
Jan am 22 Jun. 2011
Thanks Andrei,
This is indeed more elegant. No possibility to do it in-line?
Regards,
Jan
Andrei Bobrov
Andrei Bobrov am 22 Jun. 2011
I think don't possible
Teja Muppirala
Teja Muppirala am 22 Jun. 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 am 22 Feb. 2024
Bearbeitet: Stephen23 am 22 Feb. 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Operators and Elementary Operations finden Sie in Hilfe-Center und File Exchange

Produkte

Gefragt:

Jan
am 22 Jun. 2011

Bearbeitet:

am 22 Feb. 2024

Community Treasure Hunt

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

Start Hunting!

Translated by