1
votes

I have a matrix that looks like this (10 x 8) and I need to reshape to a "variable row-length" but same # of columns such as the following showing my current matrix first:

   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

and I need it to be trimmed so that I can plot the uneven columns beginning at the common point, which == 1.0000. Final array looks like this so that each new column begins with 1.0000 and has the values following each 1.0000 directly below in columns:

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
Most plotting functions in MATLAB ignore NaN, so your matrix could work as-is...How are you trying to plot it?Rody Oldenhuis
Yes, the NaN is ignored in the basic plot but the plotted values(cols) all begin on the x-axis based on where they are positioned in the row and column space in the original matrix. I need the plots all to start on the x-axis at x==1 and I have many other matrices to plot just like this so I'm looking for a solution to apply generally to other matrices like the original matrix shown here. thanks-user2100039

3 Answers

2
votes

You can move the NaN values to the end of column and avoid having different sizes for columns. This way the plot function works just fine. Here is one way of doing it:

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
votes

You can shift the NaNs a the bottom of your matrix, which I called A:

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

% then simply plot
plot(B)
1
votes

Matlab doesn't support variable length matrices. You will need to create a cell, which will need a different (maybe custom) plot function. How do you think such a plot will look? As Rody says, many plot functions ignore NaNs. Some basic code for creating such a cell would be:

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

Alternatively

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

Would remove any Inf values as well as any NaNs.

EDIT: In response to your comment, a function to plot as you have described would be:

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