修剪MATLAB阵列保持列

时间:2013-06-21 14:50:55

标签: matlab matrix

我有一个看起来像这样的矩阵(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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

Matlab不支持可变长度矩阵。您将需要创建一个单元格,这将需要一个不同的(可能是自定义的)绘图功能。您如何看待这样的情节?正如Rody所说,许多情节函数忽略了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

答案 2 :(得分:1)

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

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

% then simply plot
plot(B)