0

文書化されていないMatlabコードがいくつか与えられ、それが何をするのかを理解しようとしています。コードに散在するコメントとして、主な質問を以下に示します。

% x is an array, I believe of dimension (R, 2)
% I understand that the following line creates a logical array y of the same 
% dimensions as x in which every position that has a number (i.e. not Nan) 
% contains True.
y=~isnan(x)

for k=1:R
   % I don't know if z has been previously defined. Can't find it anywhere 
   % in the code I've been given. I understand that z is a cell array. The
   % logical array indexing of x, I understand basically looks at each row
   % of x and creates a 1-column array (I think) of numbers that are not Nan,
   % inserting this 1-column array in the kth position in the cell array z.
   z{k}=x(k, y(k,:))
end
% MAIN QUESTION HERE: I don't know what the following two lines do. what 
% will 'n' and 'm' end up as? (i.e. what dimensions are 'd'?) 
d=[z{:,:}]
[m,n]=size(d)
4

1 に答える 1

3

についてy=~isnan(x)、あなたは正しいです。

の行は、 の番目の行にあるx(k,y(k,:))non-Nans を示します。そのため、 (奇妙な方法で)の non-Nans 値を収集しているようです。列の論理インデックスとして機能することに注意してください。ここで、 「その列を含める」ことを意味し、「含めない」ことを意味します。kxzxy(k,:)truefalse

最後の質問について[z{:,:}]は、この場合は と同等です。これは、[z{:}]次元zが 1 つしかなく、セル配列 の内容を水平方向に連結するためzです。たとえば、z{1} = [1; 2]; z{2} = [3 4; 5 6];それで が得られ[1 3 4; 2 5 6]ます。したがって、は を構成する行列m一般的な行数になり、 は列数の合計になります (私の例では 2 になり、3 になります)。そのような共通の行数がない場合、エラーが発生します。たとえば、if thenまたは エラーを返します。znmnz{1} = [1 2]; z{2} = [3 4; 5 6];[z{:}][z{:,:}]

したがって、最終結果は、行を増やしてから列を増やすことで順序付けさdれた not-Nans を含む単なる行ベクトルです。xそれは次のようにもっと簡単に入手できたはずです

xt = x.';
d = xt(~isnan(xt(:))).';

これは、よりコンパクトで、Matlab に似ており、おそらく高速です。

于 2013-10-04T20:46:27.753 に答える