simple Matrix manipulation without for-loop needed

조회 수: 1 (최근 30일)
Marko
Marko 2020년 12월 30일
편집: Bruno Luong 2021년 1월 10일
Hello Community,
i need your help.
I have a matrix which size is (N+1,N+1). I have also some vectors with different length. The elements of these vectors contain integer numbers.
The code above is modifying the matrix L in such a way, that it reads at first the first elemtn of vector vec and it set the row with the index = vec(i) to zero. and set a 1 on L(index,index).
This is accomblished by a slow for loop.
Do you have any ideas how it could be done in a more faster and elegent way?
function L = modifyLDirichlet(L,vec)
for i=1:numel(vec)
L(vec(i),:) = 0;
L(vec(i),vec(i)) = 1;
end
end
Thank you for your advice!
Best,
MJ

채택된 답변

Bruno Luong
Bruno Luong 2020년 12월 30일
L(vec,:) = 0;
L(sub2ind(size(L),vec,vec)) = 1;
  댓글 수: 5
Marko
Marko 2021년 1월 10일
I have to give you more detailed inforamtion about the matrix L and the vector vec:
L is a sparse matrix because i dont have enough memory.
N is between 32 and 128
and vec has a length of 2*((N+1)^2-(N-1)^2), normaly some random indices, but for demonstartion it is ok if they are linear from 1 to 2*((N+1)^2-(N-1)^2).
function dummyFun(N,k,sparsity)
% N is a whole Number between 32 and 128
% k is 1 for sub2ind and 2 for a different algorithm
% sparsity is 0 for full matrix and 1 for sparse matrix
L=([rand((N+1)^2,(N+1)^2),zeros((N+1)^2,(N+1)^2),rand((N+1)^2,(N+1)^2);...
zeros((N+1)^2,(N+1)^2),rand((N+1)^2,(N+1)^2),rand((N+1)^2,(N+1)^2);...
rand((N+1)^2,(N+1)^2),rand((N+1)^2,(N+1)^2),zeros((N+1)^2,(N+1)^2)]);
if sparsity >0
L=sparse(L);
end
vec = 1:2*((N+1)^2-(N-1)^2);
L(vec,:) = 0;
if k==1
tic
L(sub2ind(size(L),vec,vec)) = 1;
toc
elseif k==2
tic
A=diag(L); A(vec,1)=1;L=L+diag(A-diag(L));
toc
end
end
Results:
>> dummy(32,1,0); dummy(32,2,0);dummy(32,1,1); dummy(32,2,1);
Elapsed time is 0.008623 seconds.
Elapsed time is 0.048551 seconds.
Elapsed time is 3.254162 seconds.
Elapsed time is 0.079923 seconds.
Results:
>> dummy(48,1,0); dummy(48,2,0);dummy(48,1,1); dummy(48,2,1);
Elapsed time is 0.000885 seconds.
Elapsed time is 0.206215 seconds.
Elapsed time is 24.831584 seconds.
Elapsed time is 0.464289 seconds.
Results:
>> dummy(64,1,0); dummy(64,2,0);dummy(64,1,1); dummy(64,2,1);
Elapsed time is 0.000094 seconds.
Elapsed time is 0.714662 seconds.
Elapsed time is 106.211347 seconds.
Elapsed time is 1.349834 seconds.
Conclusio
Your code is faster if the matrix L is not sparse.
Bruno Luong
Bruno Luong 2021년 1월 10일
편집: Bruno Luong 2021년 1월 10일
For sparse matrix you should avoid to set non-zero elements to 0 ad vice versa at the first place.
Never repeat sufficiently: Try to build sparse matrix is recommended using only once the command
S = sparse(I, J, S, ...)
if you look for efficiency.
But I find your comment a little off-topic, you never mention you have sparse matrix in your orginal question or when you make a comment "i have found a much faster solution" coming from no-where with no information to backup.
If you want to compare or improve speed with your specific need and matrix structure, please open another thread.
I'll stop comment on side topic.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by