1

我有一个看起来像这样(10 x 8)的矩阵,我需要重塑为“可变行长”但相同的列数,如下所示,首先显示我当前的矩阵:

   NaN       NaN       NaN       NaN       NaN       NaN       NaN       NaN
   NaN       NaN       NaN       NaN    1.0000       NaN       NaN       NaN
   NaN       NaN       NaN       NaN    0.9856       NaN       NaN       NaN
   NaN       NaN       NaN    1.0000    0.9960       NaN       NaN       NaN
   NaN    1.0000       NaN    1.2324    0.9517       NaN       NaN       NaN
   NaN    1.0721       NaN    1.1523    0.8877       NaN       NaN    1.0000
   NaN    1.0617    1.0000    0.9677    1.0006       NaN       NaN    1.3116
1.0000    0.9944    0.9958    1.0712    1.0369    1.0000    1.0000    1.2027
0.9717    0.9995    0.9705    1.0691    0.8943    0.9724    0.8863    1.2083
1.0168    0.9908    0.9406    1.0460    0.8647    0.9483    0.9064    1.2035

我需要修剪它,以便我可以从公共点开始绘制不均匀的列,这 == 1.0000。最终数组看起来像这样,因此每个新列都以 1.0000 开头,并且在列的正下方每个 1.0000 后面都有值:

1.0000   1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000
0.9717   1.0721 0.9958 1.2324 0.9856 0.9724 0.8863 1.3116
1.0168   1.0617 0.9705 1.1523 0.9960 0.9483 0.9064 1.2027
         0.9944 0.9406 0.9677 0.9517               1.2083
         0.9995        1.0712 0.8877               1.2035
         0.9908        1.0691 1.0006
                       1.0460 1.0369
                              0.8943
                              0.8647
4

3 回答 3

2

您可以将NaN值移动到列的末尾,并避免列的大小不同。这样,绘图功能就可以正常工作。这是一种方法:

function C = relocateNaN(A)
C=zeros(size(A)); 
B=sum(isnan(A)); 
for k=1:size(A,2), 
    C(:,k) = [A(B(k)+1:end,k); A(1:B(k),k)]; 
end
end
于 2013-06-21T15:24:13.963 回答
1

Matlab 不支持变长矩阵。您将需要创建一个单元格,该单元格将需要不同的(可能是自定义的)绘图功能。你认为这样的情节会如何?正如罗迪所说,许多绘图函数忽略了 NaN。创建这样一个单元的一些基本代码是:

MyCell=cell(1,size(MyMatrix,2)); % Make a cell with same number of columns as your matrix
for v = 1:size(MyMatrix,2)
    MyCell{v}=MyMatrix(~isnan(MyMatrix(:,v)),v); % For each column, find the NaN values, and then select the opposite, and put it into entry "v" of the cell
end

或者

MyCell{v}=MyMatrix(isfinite(MyMatrix(:,v)),v);

将删除任何 Inf 值以及任何 NaN。

编辑:作为对您的评论的回应,您所描述的绘图功能将是:

function CellLinePlot(MyCell)
    figure;
    J=jet(length(MyCell)); %  Make a colormap with one entry for each entry in the cell
    for v=1:length(MyCell)
        line(1:length(MyCell{v}),MyCell{v},'color',J(v,:)); % draw a line with y values equal to the cell contents, and x values equal to the number of points
    end
于 2013-06-21T15:12:27.973 回答
1

您可以将 NaN 移动到矩阵的底部,我称之为A

B   = NaN(size(A));
idx = ~isnan(A);
B(flipud(idx)) = A(idx);

% then simply plot
plot(B)
于 2013-06-21T15:31:08.487 回答