How can I apply arrayfun with 2D matrix inputs?
12 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Daichi
le 26 Juin 2017
Commenté : Andrei Bobrov
le 26 Juin 2017
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 commentaires
Andrei Bobrov
le 26 Juin 2017
Modifié(e) : Andrei Bobrov
le 26 Juin 2017
It is unlikely that the arrayfun will accelerate.
Réponse acceptée
Andrei Bobrov
le 26 Juin 2017
Modifié(e) : Andrei Bobrov
le 26 Juin 2017
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 commentaires
Andrei Bobrov
le 26 Juin 2017
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]);
Plus de réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!