How to simplify the for loop for 3D matrix?

조회 수: 3 (최근 30일)
Kalasagarreddi Kottakota
Kalasagarreddi Kottakota 2023년 1월 11일
댓글: Bruno Luong 2023년 1월 11일
I have the following code, where I am looking to eliminate only the outer for-loop (k) and include 'k' inside the inner loop. Here, I perform inverse considering each calculation point (k=1:NFocP) in a loop, but would like to some how eliminate outer loop and perform the inverse for all points at one instance. And the other concern is that I don't want to disturb the mldivide (\) function. Is there any way to solve this issue?
clear all;
load A.mat
load s1.mat
load s2.mat
Foc = size(s1,3); % no of calculation points
Nt = size(s1,2); % length of signal
M = size(s1,1); % no of rsensors
for k=1:Foc
for p = 1:Nt
H = [s1(:,p,k) s2(:,p,k)];
tmp = H\ A(:,p);
Ainv1(k,p) = tmp(1);
Ainv2(k,p) = tmp(2);
end
end
  댓글 수: 2
Benjamin Thompson
Benjamin Thompson 2023년 1월 11일
That would require calculating an inverse of a 3D matrix for H which is not possible. You may be able to use foreach to speed up the code by doing the outer loops in parallel.
Kalasagarreddi Kottakota
Kalasagarreddi Kottakota 2023년 1월 11일
Attached H and A1
The matrix H looks like 9 x 2 x 14 and matrix A1 is 9 x1. I need to perform inversion between every page of H with A1 using mldivide so that output looks like 2 x 1 x14 matrix. No loops. Is this possible?
clear all;
close all;
load H.mat
load A1.mat

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

채택된 답변

Bruno Luong
Bruno Luong 2023년 1월 11일
편집: Bruno Luong 2023년 1월 11일
load('A.mat');
load('s1.mat');
load('s2.mat');
[m,n,p] = size(s1);
% s = [reshape(s1, [m,1,n,p]), reshape(s2, [m,1,n,p])];
% Ar = reshape(A,m,1,[]);
% C=pagemldivide(s,Ar);
% Ainv1=reshape(C(1,:,:),n,p).';
% Ainv2=reshape(C(2,:,:),n,p).';
s = reshape([s1;s2], [m,2,n,p]);
Ainv1 = zeros(p,n);
Ainv2 = zeros(p,n);
for i=1:n
C=pagemldivide(s(:,:,i,:),A(:,i));
Ainv1(:,i)=C(1,:);
Ainv2(:,i)=C(2,:);
end
  댓글 수: 4
Kalasagarreddi Kottakota
Kalasagarreddi Kottakota 2023년 1월 11일
편집: Kalasagarreddi Kottakota 2023년 1월 11일
Ok, I see now. I have 2021. Is there any possibility to replace pagemldivide with any other alternate functions to do same task?
Bruno Luong
Bruno Luong 2023년 1월 11일
You might try this that requires the FEX
https://fr.mathworks.com/matlabcentral/fileexchange/27762-small-size-linear-solver#functions_tab
[m,n,p] = size(s1);
s = reshape([s1;s2], [m,2,n,p]);
Ainv1 = zeros(p,n);
Ainv2 = zeros(p,n);
Ar = reshape(A,m,1,[]);
scs = pagemtimes(s,'ctranspose',s,'none');
scA = pagemtimes(s,'ctranspose',Ar,'none');
for i=1:n
scsi = reshape(scs(:,:,i,:),2,2,[]);
scAi = reshape(scA(:,:,i,:),2,1,[]);
C = inv2(scsi, scAi); % https://fr.mathworks.com/matlabcentral/fileexchange/27762-small-size-linear-solver#functions_tab
Ainv1(:,i)=C(1,:);
Ainv2(:,i)=C(2,:);
end

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by