簡體   English   中英

填充已知值以稀疏矩陣

[英]Filling known values to sparse Matrix

我正在嘗試使用涉及方程系統的FDE(有限差分法)解決Matlab中的問題。

所以我有

[A] {T} = {C}-> [A] ^(-1){C} = {T}

我“知道” [A]和{C}的所有值。 由於矩陣大部分為零,因此我使用的是稀疏矩陣。

但是Matlab在向矩陣填充已知值時會警告我。

該稀疏索引表達可能很慢。

這是一個例子:

clear;clc;
% Number of nodes.
nodes = 5000;

% My 
A = sparse(nodes,nodes);    % Known parameters.
C = sparse(nodes,1);        % Known parameters.
T = sparse(nodes,1);        % Trying to find.

% Solving equation: [A]{T}={C} -> [A]^(-1){C}={T}

% I'm trying to fill my known values to [A]

% I have 40+ 'sections' with different values. For this example I use one
% section with all values equals to 1.

Section1 = [1, 30, 50, 60, 100, 430, 4500];  % Nodes in section 1.

% Random numbers for the example. (I generate them for each node.)
q = 10;
w = 400;
e = 1000;
r = 3500;

for i = 1:nodes
    if any(Section1(:)==i)
        A(i,q) = 1;                   % Error on this line
        A(i,w) = 1;                   % Error on this line
        A(i,e) = 1;                   % Error on this line
        A(i,r) = 1;                   % Error on this line
    end
end

您可以使用行,列和值的列表構造稀疏矩陣。

例如

>> i = [1,2,3];
>> j = [2,3,4];
>> s = [10, 20, 30];
>> A = sparse(i,j,s,5,5)

A =

   (1,2)       10
   (2,3)       20
   (3,4)       30

>> full(A)

ans =

     0    10     0     0     0
     0     0    20     0     0
     0     0     0    30     0
     0     0     0     0     0
     0     0     0     0     0

如果無法提前生成ijs ,則可以使用spalloc在稀疏矩陣中預分配空間,這將加快分配速度。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM