How can I apply arrayfun with 2D matrix inputs?

I want to accelerate the following code.
B=zeros(N,M,M);
for n=1:N
B(n,:,:)=inv(A(n,:,:));
end
where the size of A is N x M x M. Arrayfun doesn't support such a matrix input. So I would like to know the way to accelerate it.
Thank you in advance!

댓글 수: 2

Andrei Bobrov
Andrei Bobrov 2017년 6월 26일
편집: Andrei Bobrov 2017년 6월 26일
It is unlikely that the arrayfun will accelerate.
Daichi
Daichi 2017년 6월 26일
Thank you. I see. Is there any way to handle this kind of processing?

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

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2017년 6월 26일
편집: Andrei Bobrov 2017년 6월 26일

0 개 추천

B = permute(A,[2,3,1]);
eye3 = eye(3);
for jj = 1:size(B,3)
B(:,:,jj) = B(:,:,jj)\eye3;
end
B = permute(B,[3,1,2]);
or within cellfun:
B = permute(cell2mat(cellfun(@(x)x\eye(3),...
num2cell(permute(A,[2,3,1]),[1,2]),'un',0)),[3,1,2]);

댓글 수: 2

Daichi
Daichi 2017년 6월 26일
편집: Daichi 2017년 6월 26일
I see that cellfun is available in this case. But I eventually found that this kind of processing cannot be accelerated even if we use cellfun as you described. I guess num2cell and cell2mat may require some computational costs. Fortunately, the first one using backslash operator was faster. Anyway, thank you so much for your kind answer.
Within arrayfun:
B = permute(A,[2,3,1]);
Bc = arrayfun(@(ii)B(:,:,ii)\eye(3),reshape(1:size(B,3),1,1,[]),'un',0);
B = permute(cell2mat(Bc),[3,1,2]);

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 行列および配列에 대해 자세히 알아보기

질문:

2017년 6월 26일

댓글:

2017년 6월 26일

Community Treasure Hunt

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

Start Hunting!