필터 지우기
필터 지우기

How to apply arrayfun on a 3D matrix?

조회 수: 10 (최근 30일)
Mark Golberg
Mark Golberg 2017년 7월 25일
댓글: Mark Golberg 2017년 8월 1일
Hello,
I have the following matrix:
size(A) = [64 64 1000];
I'd like to apply the following function on each frame, along the 3rd dimension.
lasca_func = @(x)(mean2(x)/std2(x));
How can I do this via arrayfun, and not through for-loop?
Thanks in advance!
  댓글 수: 1
Adam
Adam 2017년 7월 25일
I don't think arrayfun can be used for that - it takes the array values in turn as linear indices so the structure of the array is not preserved and the function will be applied per element for the whole 64*64*1000.

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

채택된 답변

Stephen23
Stephen23 2017년 7월 25일
편집: Stephen23 2017년 7월 25일
"How can I do this via arrayfun, and not through for-loop?"
You can't, because arrayfun operates on each element of an array. You cannot tell it to operate along specific dimensions of an array. An alternative would be to split the array into a cell array and then call cellfun, but this would be slower than using a preallocated loop.
Unless you have a gpuArray the simplest solution is to use mean and std, and specify the dimension:
>> A = rand(64,64,1000);
>> Z = mean(A,3)./std(A,0,3);
>> size(Z)
ans =
64 64
  댓글 수: 2
Adam
Adam 2017년 7월 25일
편집: Adam 2017년 7월 25일
As I understood the question the result should be a 1x1000 result though with the mean/std calculation over each 64*64 matrix.
I was thinking something similar to this, but while it works for mean you can't do this for standard deviation as it doesn't give correct results:
mean( mean( a, 1 ), 2 ) ./ std( std( a, [], 1 ), [], 2 )
Mark Golberg
Mark Golberg 2017년 8월 1일
Thanks guys.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by