1

NaN値をスキップしながら、行列に対して要素ごとの加算を実行したいと思います。MATLABとOctaveにはnansumがありますが、行列内で列ごとに加算します。

させて:

a = NaN * zeros(3)
b = ones(3)


が欲しいです:

c = nan+(a, b)

c = b


と:

d = nan+(a,a)

d = a
4

2 に答える 2

6

n + 1次元に沿ってnd配列を連結する場合は、引き続きnansumを使用できます。

2Dの場合

% commands de-nested for readability. You can do this with a single line, of course
tmp = cat(3,a,b);
c = nansum(tmp,3);

一般的なケース

function out = nansumByElement(A,B)
%NANSUMBYELEMENT performs an element-wise nansum on the n-D arrays A and B
% A and B have to have the same size

% test input
if nargin < 2 || isempty(A) || isempty(B) || ndims(A)~=ndims(B) || ~all(size(A)==size(B))
error('please pass two non-empty arrays of the same size to nansumByElement')
end

% calculate output

nd = ndims(A); % get number of dimensions
% catenate and sum along n+1st dimension
out = nansum(cat(nd+1,A,B),nd+1);
于 2009-12-19T17:17:29.197 に答える
1
a_fixed = a;
a_fixed(isnan(a)) = 0;
b_fixed = b;
b_fixed(isnan(b)) = 0;
c = a_fixed.+b_fixed;
于 2009-12-19T17:07:07.573 に答える