how to replace the following code, to get the matrix
古いコメントを表示
Hi, there! I going to structure the following matrix, which has some regulations.please see the picture below: if n=4;

if n=5;


My current code is below, which has many 'for'loops.
if true
% code
function P1=transition_probabilityP1(n)
A1={zeros(n^2,n^2)};
for i=1:n-1
for j=i+1
A1{i,j}=diag(0.407*ones(1,n));
end;
end;
for i=2:n
for j=i-1
A1{i,j}=diag(0.298*ones(1,n-1),-1);
end
end
for i=1:n
for j=i
e=diag(0.295*ones(1,n-1),1);
if i==1
f=diag(0.298*ones(1,n));
A1{1,1}=e+f;
A1{1,1}(n,n)=0.593;
elseif i==n
g=diag(0.407*ones(1,n));
A1{n,n}=e+g;
A1{n,n}(1,1)=0.705;
A1{n,n}(n,n)=0.702;
else
A1{i,j}=e;
A1{i,j}(1,1)=0.298;
A1{i,j}(n,n)=0.295;
end;
end
end
ind=cellfun('isempty',A1);
[r,c]=find(ind==1);
for i=1:length(r)
A1{r(i),c(i)}=zeros(n,n);
end
P1=cell2mat(A1);
end
Is there anyone who know how to make the same matrix with less loops, less memory and compute faster. Generally, I want compute the matrix P with dimension larger than 3,200,000 * 3,200,000 ! Thank you!
3 件のコメント
C.J. Harris
2016 年 1 月 13 日
編集済み: C.J. Harris
2016 年 1 月 13 日
A 3,200,000 x 3,200,000 matrix equals 1.024 * 10^13 elements. Assuming you want to use doubles, that's 8 bytes per element, i.e. 8.192 * 10^13 bytes total, or 81.92 TB. That's not going to work on any sort of machine you can get your hands on.
Think smaller.
Ralf
2016 年 1 月 13 日
Since the Matrix contains many zeros, consider using the sparse Matrix concepts.
See help sparse.
Jason
2016 年 1 月 14 日
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Profile and Improve Performance についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!