matlab 生成 稀疏 对称正定 矩阵
2015-04-22 12:53
471 查看
参考 matlab 读写文件程序
sparse matrix demo for krylov subspace iterative methods
% Generate SPD for CG test
clear
clc
method = 'poisson';
switch method
case
'poisson'
disp( 'Poisson Matrix');
Dim = 2;
Dim2 = Dim^2;
MatrixGen = gallery('poisson', Dim);
case
'tridiag'
disp ( 'Tridiagonal matrix');
n = 4;
c = -1;
e = -1;
d = 4;
MatrixGen = gallery('tridiag', n, c, d, e);
case
'wathen'
disp ('FEM Sparse Random Matrix');
nx = 2;
ny = 2;
MatrixGen = gallery('wathen', nx, ny);
end
totalNnz = nnz(MatrixGen);
nonzerosInRow = zeros(Dim2, 1);
for j = 1: Dim2
nonzerosInRow(j) = nnz(MatrixGen(:, j));
end
fid1 = fopen('matrix.dat',
'w');
P_full = full(MatrixGen);
for j= 1:Dim2
for i = 1:Dim2
if(P_full(i,j) ~= 0)
fprintf(fid1, '%d %d %f\n', i, j, P_full(i,j));
end
end
end
b = sprand(Dim2, 1, 0.9);
b_full = full(b);
fid2 = fopen('Bvector.dat',
'w');
for i=1:Dim2
fprintf(fid2, '%d\n', b_full(i,1));
end
assert( size(b_full,1) == size(MatrixGen,1));
fclose(fid1);
fclose(fid2);
sparse matrix demo for krylov subspace iterative methods
% Generate SPD for CG test
clear
clc
method = 'poisson';
switch method
case
'poisson'
disp( 'Poisson Matrix');
Dim = 2;
Dim2 = Dim^2;
MatrixGen = gallery('poisson', Dim);
case
'tridiag'
disp ( 'Tridiagonal matrix');
n = 4;
c = -1;
e = -1;
d = 4;
MatrixGen = gallery('tridiag', n, c, d, e);
case
'wathen'
disp ('FEM Sparse Random Matrix');
nx = 2;
ny = 2;
MatrixGen = gallery('wathen', nx, ny);
end
totalNnz = nnz(MatrixGen);
nonzerosInRow = zeros(Dim2, 1);
for j = 1: Dim2
nonzerosInRow(j) = nnz(MatrixGen(:, j));
end
fid1 = fopen('matrix.dat',
'w');
P_full = full(MatrixGen);
for j= 1:Dim2
for i = 1:Dim2
if(P_full(i,j) ~= 0)
fprintf(fid1, '%d %d %f\n', i, j, P_full(i,j));
end
end
end
b = sprand(Dim2, 1, 0.9);
b_full = full(b);
fid2 = fopen('Bvector.dat',
'w');
for i=1:Dim2
fprintf(fid2, '%d\n', b_full(i,1));
end
assert( size(b_full,1) == size(MatrixGen,1));
fclose(fid1);
fclose(fid2);
相关文章推荐
- MATLAB中稀疏矩阵转换与生成
- 随机生成某些稀疏矩阵
- 【Matlab】sparse函数和full函数(稀疏矩阵和非稀疏矩阵转换)
- matlab 稀疏矩阵(sparse matrix)
- matlab meshgrid函数生成二维网格矩阵举例讲解
- 特征提取——生成样本矩阵(MATLAB实现)
- matlab稀疏矩阵创建及实例
- matlab,可视化稀疏矩阵的数据结构(即非零元位于哪些位置)
- matlab 稀疏矩阵
- Matlab命令集——稀疏矩阵函数
- MATLAB笔记 sparse稀疏矩阵函数
- Java调用Matlab生成的函数,参数为矩阵怎么传递
- 一个简单的MATLAB读写文本文件程序--稀疏矩阵向量乘法
- Matlab与线性代数 -- 稀疏矩阵的图形显示
- 【matlab】函数meshgrid的用法详解(生成网格矩阵)和ndgrid的区别及用法
- matlab中生成随机正交矩阵
- 对称矩阵、稀疏矩阵及矩阵的逆置与加法
- matlab 快捷的生成矩阵
- 对称矩阵及稀疏矩阵的压缩
- matlab meshgrid函数 生成网格矩阵