MATLAB: Matrix formation from two given matrix

matrix manipulation

i want to form a diagonal matrix from two matrix, one contains the value of diagonal elements and other contains how many times it should occur.For example: a=[5 10 15] and b=[2 2 2] so the resultant matrix should look like r=[5 0 0 0 0 0;0 5 0 0 0 0;0 0 10 0 0 0;0 0 0 10 0 0;0 0 0 0 15 0;0 0 0 0 0 15]; But the problem is a and b can vary depending upon the situation or user input.
can i form such a matrix? I will highly appreciate any kind of suggestion.
Thanks, Mamun

Best Answer

"for" loop around repmat() in order to build the diagonal vector, and then diag() that.
Or just use a nested "for" loop.