Thread Subject: help with blkdiag()

Subject: help with blkdiag()

From: Tim Yang

Date: 12 Jun, 2009 21:52:01

Message: 1 of 2

I have a 2D matrix

  1 2 3 4 5
  6 7 8 9 10
 11 12 13 14 15
 16 17 18 19 20

I want to create a diagonal matrix without using loops and using blkdiag()
ie matrix
  1 2 3 4 5 alll zero
  0 0 0 0 0 6 7 8 9 10
  0 0 0 0 0 0 0 0 0 0 11 12 13 14 15
  0 0 0 ... 16 17 18 19 20

I don't want to convert it to cell array (because the matrix is large, conversion is time consuming). blkdiag() only accept ( row vector, row vector...)

Thanks
Tim

Subject: help with blkdiag()

From: Bruno Luong

Date: 12 Jun, 2009 22:08:01

Message: 2 of 2

A = [1 2 3 4 5;
  6 7 8 9 10;
 11 12 13 14 15;
 16 17 18 19 20 ];

I = repmat((1:size(A,1)), size(A,2),1);
J = 1:numel(A);
B = accumarray([I(:) J(:)],reshape(A.',[],1)) % sparse is even better

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
blkdiag Sprinceana 22 Jun, 2009 06:58:42
rssFeed for this Thread

Contact us at files@mathworks.com