Borrar filtros
Borrar filtros

Taking expectation of 3 dimension matrix

2 visualizaciones (últimos 30 días)
Mert Demir
Mert Demir el 23 de Feb. de 2022
Respondida: Neelanshu el 6 de Nov. de 2023
Hi,
I am trying to take the expectation of a function on two variables where the probabilities are markov process and it is 3dimension.
E(v(a',b',c'))= Sum(sum(v(a,b,c)*Q(b,b')*Q(c,c'))) where sums are on b and c and Q are the transition matrices.
I found it as below but i need to make it faster.
Nk=100, Nz=5, Ne=4
v_n=rand(Nk,Nz,Ne)
Q_z(Nz,Nz) is 5*5 transition matrix where sum of each row makes 1
Q_e(Ne,Ne) is 3*3 transition matrix where sum of each row makes 1
for ik=1:Nk
for iz=1:Nz
for ie=1:Ne
asd(ik,iz,ie)=dot(Q_e(ie,:),squeeze(v_n(ik,iz,:)));
end
end
end
for ik=1:NkPts
for iz=1:Nz
for ie=1:Ne
expv_n(ik,iz,ie)= dot(Q_z(iz,:),squeeze(asd(ik,:,ie)));
end
end
end

Respuestas (1)

Neelanshu
Neelanshu el 6 de Nov. de 2023
Hi Mert Demir,
I understand that you are facing an issue related to finding a faster way to compute the expectation of 3 dimensional matrix.
The “for” loops in the code when converted to matrix operations will be much faster. For instance, replacing
for ie=1:Ne
asd(ik,iz,ie) = dot(Q_e(ie,:),squeeze(v_n(ik,iz,:)));
end
with
asd(ik,iz,:) = dot(Q_e,repmat(squeeze(v_n(ik,iz,:))',4,1),2);
will improve the computation time significantly.
You can refer the following link to transform the remaining “for” loops with matrix operations: https://www.mathworks.com/help/matlab/matrices-and-arrays.html?s_tid=CRUX_lftnav
Hope it helps,
Regards,
Neelanshu

Categorías

Más información sobre Multidimensional Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by