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,:))行将给出k第 - 行中的非南x。所以似乎z正在收集x(以一种奇怪的方式)的非南斯值。请注意,它y(k,:)充当列的逻辑索引,其中true表示“包含该列”并false表示“不包含”。

至于你的最后一个问题:[z{:,:}]在这种情况下相当于[z{:}], 因为z只有一维,它将水平连接单元格数组的内容z。例如,用z{1} = [1; 2]; z{2} = [3 4; 5 6];它会给[1 3 4; 2 5 6]. 因此m,将是构成 的矩阵中的共同z行数,并且n将是列数的总和(在我的示例m中为 2 和n3)。如果没有这样常见的行数,则会出错。例如,如果z{1} = [1 2]; z{2} = [3 4; 5 6];then[z{:}][z{:,:}]给出错误。

所以最终结果d只是一个行向量,其中包含x通过增加行然后增加列排序的非Nans。这本可以更容易获得,因为

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

它更紧凑,更像 Matlab,并且可能更快。

于 2013-10-04T20:46:27.753 回答