Apply function on layer of 3D Array

22 visualizaciones (últimos 30 días)
Jonatan Menger
Jonatan Menger el 8 de Mayo de 2020
Comentada: Jonatan Menger el 19 de Mayo de 2020
Hello,
Assume I have the following 3D Array Hv:
v=reshape(rand(1,6),1,1,[]);
H=[1 0 0 0; 0 4 4 0; 0 4 4 0; 0 0 0 1];
Hv=bsxfun(@times,H,v);
I want to perform the operation expm() on each layer of Hv. So the output should be an array of the same dimension, with the expm(H) of every layer. I do not want to use a loop over the third dimension, and hope it is possible to perform the operation directly on the vector. I hope my question is understandable, I couldn't find a solution anywhere. Thanks alot.
  3 comentarios
Stephen23
Stephen23 el 11 de Mayo de 2020
"I do not want to use a loop over the third dimension"
A loop is likely to be the fastest and most efficient solution. Why do you wish to avoid a loop?
Jonatan Menger
Jonatan Menger el 19 de Mayo de 2020
Thanks for the answers. I was trying to improve the speed of my program which has three nested loops, with the inner one doing 12000 rounds each time. I thought, as matlab is optimized for working with arrays, that there is some way to do it more efficient. But maybe i'll just need to except the speed i've got now.

Iniciar sesión para comentar.

Respuestas (1)

Monika Phadnis
Monika Phadnis el 11 de Mayo de 2020
You can use 'arrayfun' function for your use case in the following way:
>> data_expm = arrayfun(@(i)expm(Hv(:,:,i)), 1:size(Hv,3), 'UniformOutput', false)
You can refer this answer for more information :
  1 comentario
Rik
Rik el 11 de Mayo de 2020
As mentioned there, it is probably faster to use a loop instead of this.

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing 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