Apply function on layer of 3D Array
이전 댓글 표시
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
Rik
2020년 5월 8일
You will probably have to either use a loop, or replicate your data to use functions like cellfun to hide the loop. I don't really see a way around it.
Stephen23
2020년 5월 11일
"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
2020년 5월 19일
답변 (1개)
Monika Phadnis
2020년 5월 11일
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
Rik
2020년 5월 11일
As mentioned there, it is probably faster to use a loop instead of this.
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!