MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

# Thread Subject: multiplying diag matrix with a reg matrix

 Subject: multiplying diag matrix with a reg matrix Date: 4 Apr, 2009 20:33:01 Message: 1 of 9 I need to perform the product D*A where D is a diagonal matrix whose diagonal elements are non-zero and A a regular matrix with most of the elements non-zero. Is there is an operation or one-line statement that performs this taking into consideration that D is diagonal instead doing a literal matrix multiplication. This would save a lot of time for me. regs bala
 Subject: multiplying diag matrix with a reg matrix From: Bruno Luong Date: 4 Apr, 2009 20:42:01 Message: 2 of 9 "Balachander Narasimhan" wrote in message ... > I need to perform the product D*A where D is a diagonal matrix whose diagonal elements are non-zero and A a regular matrix with most of the elements non-zero. Is there is an operation or one-line statement that performs this taking into consideration that D is diagonal instead doing a literal matrix multiplication. This would save a lot of time for me. > > regs > bala bsxfun(@times,diag(D),A) OR sparse(D)*A Bruno
 Subject: multiplying diag matrix with a reg matrix From: Roger Stafford Date: 4 Apr, 2009 20:48:01 Message: 3 of 9 "Balachander Narasimhan" wrote in message ... > I need to perform the product D*A where D is a diagonal matrix whose diagonal elements are non-zero and A a regular matrix with most of the elements non-zero. Is there is an operation or one-line statement that performs this taking into consideration that D is diagonal instead doing a literal matrix multiplication. This would save a lot of time for me. > > regs > bala   Turn D into a column vector and try the 'bsxfun' function with @times. It might be faster. Roger Stafford
 Subject: multiplying diag matrix with a reg matrix From: Matt Fig Date: 4 Apr, 2009 20:57:01 Message: 4 of 9 A = round(rand(1000)*10000); D = diag(randperm(1000)); There are several ways to do this. Here are two shown in comparison. N = 1000; A = round(rand(N)*N^2); D = diag(randperm(N)); tic H = D*A; toc tic C = bsxfun(@times,A,D(1:N+1:end).'); % Or use diag(D) toc tic E = sparse(D)*A; toc all(H(:)==C(:)) all(H(:)==E(:))
 Subject: multiplying diag matrix with a reg matrix From: Tim Davis Date: 5 Apr, 2009 13:08:01 Message: 5 of 9 "Matt Fig" wrote in message ... > A = round(rand(1000)*10000); > D = diag(randperm(1000)); > > There are several ways to do this. Here are two shown in comparison. > > > > > > N = 1000; > A = round(rand(N)*N^2); > D = diag(randperm(N)); > > tic > H = D*A; > toc > > tic > C = bsxfun(@times,A,D(1:N+1:end).'); % Or use diag(D) > toc > > tic > E = sparse(D)*A; > toc > > all(H(:)==C(:)) > all(H(:)==E(:)) Except that creating a dense matrix D then doing sparse(D) is slow. Better would be d = randperm(N); tic E = sparse(1:n,1:n,d)*A; toc which never forms the dense D matrix, and is much faster than E = sparse(D)*A;
 Subject: multiplying diag matrix with a reg matrix From: Matt Fig Date: 5 Apr, 2009 16:00:03 Message: 6 of 9 "Tim Davis" wrote in message > Except that creating a dense matrix D then doing sparse(D) is slow. Better would be > > d = randperm(N); > tic > E = sparse(1:n,1:n,d)*A; > toc > > which never forms the dense D matrix, and is much faster than > > E = sparse(D)*A; Very true Tiim, but the OP stated he already had the diagonal matrix. It turns out that my answer was the same as Bruno's (that's what I get for not hitting refresh on the browser before posting), so it seems we both took the problem as starting from that point.
 Subject: multiplying diag matrix with a reg matrix From: Tim Davis Date: 6 Apr, 2009 02:04:01 Message: 7 of 9 "Matt Fig" wrote in message ... > "Tim Davis" wrote in message > > Except that creating a dense matrix D then doing sparse(D) is slow. Better would be > > > > d = randperm(N); > > tic > > E = sparse(1:n,1:n,d)*A; > > toc > > > > which never forms the dense D matrix, and is much faster than > > > > E = sparse(D)*A; > > > > Very true Tiim, but the OP stated he already had the diagonal matrix. It turns out that my answer was the same as Bruno's (that's what I get for not hitting refresh on the browser before posting), so it seems we both took the problem as starting from that point. True. I didn't mean to sound like I was critiquing your reply. I just meant to suggest to the OP that it's not a good idea to from the dense version of the D matrix in the first place.
 Subject: multiplying diag matrix with a reg matrix From: Matt Fig Date: 6 Apr, 2009 02:26:03 Message: 8 of 9 "Tim Davis" wrote in message > True. I didn't mean to sound like I was critiquing your reply. > > I just meant to suggest to the OP that it's not a good idea to from the dense version of the D matrix in the first place. O.k., but I don't mind a good critiquing now and then! : )
 Subject: multiplying diag matrix with a reg matrix Date: 6 Apr, 2009 22:01:08 Message: 9 of 9 hi all, tnx for the replies, that solves my problem. regs bala "Matt Fig" wrote in message ... > "Tim Davis" wrote in message > > True. I didn't mean to sound like I was critiquing your reply. > > > > I just meant to suggest to the OP that it's not a good idea to from the dense version of the D matrix in the first place. > > O.k., but I don't mind a good critiquing now and then! : )