# Sparse matrix in optimization

4 Ansichten (letzte 30 Tage)
Tsinghua THU am 1 Aug. 2017
Bearbeitet: Tsinghua THU am 1 Aug. 2017
Matlab sparse matrix is powerful and quite efficient. I am working on some optimization problems where in each iteration a large sparse matrix, say A, is generated using:
A=sparse(iA,jA,sA,nA,nA).
During each iteration, iA and jA do not change and are exactly the same as in the first iteration, but sA changes in every iteration. So one can find that the sparsity mode is unchanged, we simply need to fill in the new sA into the place of the old sA. But in the current version, I am afraid this is impossible, i.e. in each iteration I have to explicitly generate a totally new sparse matrix using
A=sparse(iA,jA,sA,nA,nA).
Is there any way to get around explicitly generating A time and time again (since this would be time-consuming)? Any comments or advices are the most welcome on this issue.
------------------below I attach a sample code to clarify the problem----------
function y = SampleFuc(sA)
% input sA is of size 1e4*1
persistent iA jA b
if isempty(iA)
iA = 1:1e4;
jA = 1:1e4;
b = ones(1e4,1);
end
A = sparse(iA,jA,sA);
y = A\b;
end
##### 1 KommentarKeine anzeigenKeine ausblenden
Jan am 1 Aug. 2017
Please post the corresponding code. Where do you create this array? Where is it used? Why do you assume that a new creation is required in each iteration? How large is the array?

Melden Sie sich an, um zu kommentieren.

### Antworten (1)

José-Luis am 1 Aug. 2017
Bearbeitet: José-Luis am 1 Aug. 2017
Looping is one way to go.
i = [900 1000];
j = [900 1000];
v = [10 100];
S = sparse(i,j,v,1500,1500)
for ii = [i;j]
S(ii(1),ii(2)) = rand;
end
I am not sure this would be faster than creating the matrix from scratch. Should be tested.
##### 0 Kommentare-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Find more on 求解器输出和迭代输出 in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!