필터 지우기
필터 지우기

Can I calculate the inverse of a matrix using arrayfun?

조회 수: 2 (최근 30일)
Benson Gou
Benson Gou 2021년 6월 25일
댓글: Benson Gou 2021년 6월 29일
Dear All,
I have a sparse matrix A. I want to calculate its inverse. I used the following way to calculate invA.
invA = A \ speye(size(A));
My matrix size is 3000 by 3000. I found it took me 1.3 seconds to get invA. It is longer than I expected.
I am wondering if I could use arrayfun to calculate invA. The idea is to calculate each column in invA by invA(:,i) = A \ ei, where ei is a zero column vector except its ith element is 1.0.
I tried the following code to implement the above idea:
invA = arrayfun(@(x) A\x ,speye(size(A,1)));
But I got error message.
Thanks a lot.
Benson
  댓글 수: 1
Matt J
Matt J 2021년 6월 25일
I don't see how arrayfun could help, unless this is on the GPU.

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

채택된 답변

Walter Roberson
Walter Roberson 2021년 6월 25일
편집: Walter Roberson 2021년 6월 25일
invA = cell2mat(arrayfun(@(C) A\sparse(C,1,1,size(A,1),1), 1:size(A,2), 'uniform', 0))
I would be surprised if it is faster.
  댓글 수: 8
Benson Gou
Benson Gou 2021년 6월 29일
Hi, Walter,
I do not have the Image Processing Toolbox. So I cannot run decompose(A).
But the goal that I want to obtain the inverse of A is becaus I need to find out the nonzero elements in invA. So I do not know if decompose(A) is able to be useful for my case.
Thanks a lot.
Benson

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

추가 답변 (1개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021년 6월 25일
편집: Sulaymon Eshkabilov 2021년 6월 25일
In fact, you can employ arrayfun for the matric inverse calc, e.g.:
tic;
iA =arrayfun(@inv,A);
toc;
Note that arrayfun is not the best option and does not take the sparse matrix.
Just direct inv() is the fastest so far.
In fact, for solving linear systems, to compute the inverse is not advised.
  댓글 수: 4
Walter Roberson
Walter Roberson 2021년 6월 25일
By the way, you cannot apply arrayfun() directly to a sparse matrix.
Benson Gou
Benson Gou 2021년 6월 29일
Hi, Walter,
Yes, the decomposition is faster than \ operator. Thanks a lot.
Benson

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

카테고리

Help CenterFile Exchange에서 Sparse Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by