arrayfun doesn't work with rmoutliers

3 次查看(过去 30 天)
Goal
Remove outliers along dimension d of a 3D matrix using arrayfun and rmoutliers (or superior alternatives).
Minimal Working Example
Take the following matrix:
A = [0 2 0 0 0; 0 0 0 4 0; 3 0 0 3 0];
A(:,:,2) = A
A =
A(:,:,1) = 0 2 0 0 0 0 0 0 4 0 3 0 0 3 0 A(:,:,2) = 0 2 0 0 0 0 0 0 4 0 3 0 0 3 0
Since each 1D array along dimension d will have a different number of outliers, the output of arrayfun will have to be a cell array (by specifying "UniformOutput" = false). Calling rmoutliers through arrayfun returns the original matrix with the outliers (undesired behaviour):
arrayfun(@rmoutliers,A,"UniformOutput",false)
ans = 3×5×2 cell array
ans(:,:,1) = {[0]} {[2]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[4]} {[0]} {[3]} {[0]} {[0]} {[3]} {[0]} ans(:,:,2) = {[0]} {[2]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[4]} {[0]} {[3]} {[0]} {[0]} {[3]} {[0]}
The function works as expected when called on each row by itself:
rmoutliers(A(1,:,1)) % removes the 2
ans = 1×4
0 0 0 0
rmoutliers(A(3,:,1)) % removes both 3's
ans = 1×3
0 0 0
Hopefully I'm missing something trivial.

采纳的回答

Voss
Voss 2022-1-5
A = [0 2 0 0 0; 0 0 0 4 0; 3 0 0 3 0];
A(:,:,2) = A
A =
A(:,:,1) = 0 2 0 0 0 0 0 0 4 0 3 0 0 3 0 A(:,:,2) = 0 2 0 0 0 0 0 0 4 0 3 0 0 3 0
C = num2cell(A,2)
C = 3×1×2 cell array
C(:,:,1) = {[0 2 0 0 0]} {[0 0 0 4 0]} {[3 0 0 3 0]} C(:,:,2) = {[0 2 0 0 0]} {[0 0 0 4 0]} {[3 0 0 3 0]}
cellfun(@rmoutliers,C,"UniformOutput",false)
ans = 3×1×2 cell array
ans(:,:,1) = {[0 0 0 0]} {[0 0 0 0]} {[ 0 0 0]} ans(:,:,2) = {[0 0 0 0]} {[0 0 0 0]} {[ 0 0 0]}

更多回答(1 个)

Mike Croucher
Mike Croucher 2022-1-5
arrayfun operates on every element of the array. So
arrayfun(@rmoutliers,A,"UniformOutput",false)
is like doing
rmoutliers(0)
rmoutliers(0)
rmoutliers(3)
etc
  1 个评论
Tom K.
Tom K. 2022-1-5
编辑:Tom K. 2022-1-5
Hi Mike,
I was able to produce the desired results using @Benjamin's solution.
Thanks!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by