注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Mr.Right

不顾一切的去想,于是我们有了梦想。脚踏实地的去做,于是梦想成了现实。

 
 
 

日志

 
 
关于我

人生一年又一年,只要每年都有所积累,有所成长,都有那么一次自己认为满意的花开时刻就好。即使一时不顺,也要敞开胸怀。生命的荣枯并不是简单的重复,一时的得失不是成败的尺度。花开不是荣耀,而是一个美丽的结束,花谢也不是耻辱,而是一个低调的开始。

网易考拉推荐

spdiags-由对角线创建条带sparse matrix  

2012-09-24 13:42:37|  分类: 学习 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

A = spdiags(B,d,m,n) creates an m-by-n sparse matrix by taking the columns of B and placing them along the diagonals specified by d. 采用B的各个列作为A的对角线,B的列数应该和d的长度相等

 

例1:

The most common type of sparse matrix is a banded matrix, that is, a matrix with a few nonzero diagonals. Such a matrix can be created with the spdiags command. Consider the following matrix:

>> A = [ 64   -16     0   -16     0     0     0     0     0
   -16    64   -16     0   -16     0     0     0     0
     0   -16    64     0     0   -16     0     0     0
   -16     0     0    64   -16     0   -16     0     0
     0   -16     0   -16    64   -16     0   -16     0
     0     0   -16     0   -16    64     0     0   -16
     0     0     0   -16     0     0    64   -16     0
     0     0     0     0   -16     0   -16    64   -16
     0     0     0     0     0   -16     0   -16    64]
This is a 9-by-9 matrix with 5 nonzero diagonals. In Matlab's indexing scheme, the nonzero diagonals of A are numbers -3, -1, 0, 1, and 3 (the main diagonal is number 0, the first subdiagonal is number -1, the first superdiagonal is number 1, and so forth). To create the same matrix in sparse format, it is first necessary to create a  9-by- 5matrix containing the nonzero diagonals of A. Of course, the diagonals, regarded as column vectors, have different lengths; only the main diagonal has length 9. In order to gather the various diagonals in a single matrix, the shorter diagonals must be padded with zeros. The rule is that the extra zeros go at the bottom for subdiagonals and at the top for superdiagonals. Thus we create the following matrix:

>> B = [
   -16   -16    64     0     0
   -16   -16    64   -16     0
   -16     0    64   -16     0
   -16   -16    64     0   -16
   -16   -16    64   -16   -16
   -16     0    64   -16   -16
     0   -16    64     0   -16
     0   -16    64   -16   -16
     0     0    64   -16   -16
];
(notice the technique for entering the rows of a large matrix on several lines). The spdiags command also needs the indices of the diagonals:
>> d = [-3,-1,0,1,3];
The matrix is then created as follows:
>> S = spdiags(B,d,9,9);

>> full(S)
The last two arguments give the size of S.
Perhaps the most common sparse matrix is the identity. Recall that an identity matrix can be created, in dense format, using the command eye. To create the n-by-n identity matrix in sparse format, use I = speye(n).

Another useful command is spy, which creates a graphic displaying the sparsity pattern of a matrix. For example, the above penta-diagonal matrix A can be displayed by the following command; see Figure below:

>> spy(S)   % 用spy(A)也可以

例2:

B = repmat((1:6)',[1 7]); %B的各列作为A的对角线

spdiags-由对角线创建条带sparse matrix - 阿英 - Mr.Right

 

d = [-4 -2 -1 0 3 4 5];  %我以前没想到d的长度要与B的列数相等
A = spdiags(B,d,6,6);
full(A); spy(A)

 

例3:

This example illustrates the use of the syntax A = spdiags(B,d,m,n), under three conditions:

  • m is equal to n

  • m is greater than n

  • m is less than n

>>B =[
   1    6   11
   2    7   12
   3    8   13
   4    9   14
   5   10   15]

Part 1 — m is equal to n.    A矩阵的主对角线以上的super对角线取自B的列下半部分

A(3,1), A(4,2), and A(5,3) are taken from the upper part of B(:,1).

A(1,3), A(2,4), and A(3,5) are taken from the lower part of B(:,3).

 >>A = full(spdiags(B, [-2 0 2], 5, 5))

A =

     6     0    13     0     0
     0     7     0    14     0
     1     0     8     0    15
     0     2     0     9     0
     0     0     3     0    10

 

Part 2 — m is greater than n.    A矩阵的主对角线以上的super对角线取自B的列下半部分

>> A = full(spdiags(B, [-2 0 2], 5, 4))

A =

     6     0    13     0
     0     7     0    14
     1     0     8     0
     0     2     0     9
     0     0     3     0

Part 3 — m is less than n.   A矩阵的主对角线以上的super对角线取自B的列的上半部分

A(3,1) and A(4,2) are taken from the lower part of B(:,1).

A(1,3), A(2,4), and A(3,5) are taken from the upper part of B(:,3).

A = full(spdiags(B, [-2 0 2], 4, 5))

A =

     6     0    11     0     0
     0     7     0    12     0
     3     0     8     0    13
     0     4     0     9     0

  评论这张
 
阅读(1980)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016