How to avoid writing a for loop in A\b when A and b are different dimensions
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Matthew Kehoe
am 18 Feb. 2023
Kommentiert: Matt J
am 18 Feb. 2023
I am currently solving
where A is a 3d complex array of dimension
and b is a 2d real array of dimension
. In order to do this, I create a single for loop which calls linsolve()
times.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1299765/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1299770/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1299775/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1299780/image.png)
Could I avoid writing this for loop so that I wouldn't have to spend the extra computation calling linsolve()
times? It appears that the pagemldivide() function might be one way of doing this.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1299780/image.png)
clear all; close all;
% Original Method
Nx = 32;
A = complex(rand(Nx+1,Nx+1,Nx),rand(Nx+1,Nx+1,Nx)); % A is a 3D complex array
b = rand(Nx+1,Nx); % b is a 2D real array
soln = zeros(Nx,Nx+1); % the resulting solution will be a 2d complex array
for j=1:Nx
prob = linsolve(A(:,:,j),b(:,j)); % A\b
soln(j,:) = prob.'; % Size (Nx,Nx+1)
end
% New Method
soln_tilde = pagemldivide(A(:,:).',b(:)); % Size (Nx+1,1)
Is there a way of using pagemldivide() (or a seperate Matlab function) to avoid writing a for loop and get the same resulting solution of size
?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1299795/image.png)
0 Kommentare
Akzeptierte Antwort
Matt J
am 18 Feb. 2023
Bearbeitet: Matt J
am 18 Feb. 2023
soln=pagemldivide(A,reshape(b,[],1,Nx));
soln=soln(:,:).';
2 Kommentare
Matt J
am 18 Feb. 2023
Wow! I think there is an extra comma after the second colon.
There was, but I fixed it. It should work now. If it does, please Accept-click the answer.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Operating on Diagonal Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!