apply function along dimension

조회 수: 12 (최근 30일)
Daniele Rocchetta
Daniele Rocchetta 2016년 7월 6일
편집: Daniele Rocchetta 2016년 7월 7일
Hi,
I am trying to find a way to determine if all elements in a hyperplane of an n-dimensional array are NaNs. To exemplify, with a 4-D array of size (3,5,2,7), I would like to determine along dimension 2 which of the 5 sub-arrays (hyperplanes) of size (3,1,2,7) are filled with just NaNs. The result should be a 5 elems vector (the size of the original array along dimension 2) containing true or false depending on whether there are just NaNs or not in the corresponding sub-array. The for loop answer would be
% A = <3,5,2,7>
z = false(size(A,2),1);
for i = 1:size(A,2)
% turning each sub-array in a column vector and perform the check
z(i) = all(isnan(reshape(A(:,i,:,:),prod(size(A(:,i,:,:))),1)));
end
Since the arrays I work with are quite large this seems impractical, plus the dimension along which to perform the check is not given at the outset, but itself an argument of the function (i.e. A(:,i,:,:) would not work, because it should be A(:,:,i,:) if the check was performed along dimension 3). I think bsxfun and/or arrayfun could help in this regard, but their complex implementation doesn't help. Any suggestions on how this might be achieved?
Thanks in advance
Dan

채택된 답변

James Tursa
James Tursa 2016년 7월 6일
편집: James Tursa 2016년 7월 6일
Does this do what you want?
% x = nD array, k = dimension to check for all NaN's per plane
function result = nanplane(x,k)
z = 1:numel(size(x));
X = permute(x,[z(k) z(1:k-1) z(k+1:end)]);
X = reshape(X,size(X,1),[]);
result = all(isnan(X),2);
end
  댓글 수: 3
James Tursa
James Tursa 2016년 7월 7일
편집: James Tursa 2016년 7월 7일
If you are working with very large arrays, it might be a bit faster to do the isnan part first. That way the permute would be working with 1-byte data instead of 8-byte data and the memory copies involved might be a bit quicker to accomplish. E.g. something like this (CAUTION: untested):
% x = nD array, k = dimension to check for all NaN's per plane
function result = nanplane(x,k)
N = isnan(x); % <-- do the isnan first
z = 1:numel(size(x));
N = permute(N,[z(k) z(1:k-1) z(k+1:end)]); % <-- permute the 1-byte data
N = reshape(N,size(N,1),[]);
result = all(N,2);
end
Daniele Rocchetta
Daniele Rocchetta 2016년 7월 7일
편집: Daniele Rocchetta 2016년 7월 7일
Hi and thanks for the suggestion; I had actually thought the same and implemented it already, so I can confirm it's tested and working, down to the degenerate cases of vectors and scalars for input array x. Brilliant.
All the best
Daniele

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

추가 답변 (1개)

Daniele Rocchetta
Daniele Rocchetta 2016년 7월 6일
편집: Daniele Rocchetta 2016년 7월 6일
Hi James,
at a first glance and trial run I would say yes! It is quite late here now, so I'll put it under thorough testing in the morning. But before that I wanted to thank you for your timely and kind reply. I'll keep you posted
Best regards
Dan

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by