0

I need to generate different block diagonal matrices like:

blkdiag(0,0,0, ... ,0,unit)

blkdiag(0,0,0, ... ,0,unit,unit)

.

.

.

blkdiag(unit,unit, ... ,unit,unit)

in say 100 iterates and evaluate them with unit being a m*m matrix. I generate the string of the argument in a loop, however function does not understand char input and I don't know what to do!

any help is appreciated ...

1 Answer 1

2

blkdiag wants numeric values as inputs in a comma-separated list. An easy way to generate a comma-separated list is using cell arrays.

I assume that by "100 iterates" you mean that you're passing 100 values to blkdiag, with the first call being 99 zeros and one instance of unit, and the last call being 100 instances of unit.

unit = magic(5);   % some random test matrix for unit
total_iterations = 100;   % total number of iterations to go through
C = cell(2*total_iterations-1);     % total size of cell array
C(1:total_iterations-1) = 0;
C(total_iterations:end) = unit;
for iter = 1:total_iterations
   new_matrix = blkdiag(C{iter:iter+total_iterations-1});
   % do other stuff here...
end

C{a:b} generates a comma-separated list of the elements of C, which is just what we need.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you ... cell is exactly what i needed ... didn't know about it ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.