I have a matrix C.

I want to add a calculated value in a box where there is NaN.

The solution is the matrix B.

I have the matrix and vector Ampiezza = [ 0.5 0.6 0.7]

C = [               
    0.41 6.36   15.44
    0.28 6.37   15.03
    0.25 6.05   14.90
    0.25 6.05   13.50
    NaN  6.05   12.64
    NaN  6.05   12.19
    NaN  5.09   11.81
    NaN  4.45   9.29
    NaN  3.82   9.23
    NaN  3.82   8.82
    NaN  3.50   8.43
    NaN  2.54   8.18
    NaN  2.22   8.15
    NaN  NaN    5.60
    NaN  NaN    NaN
    NaN  NaN    NaN
    NaN  NaN    NaN 
                          ]

Ampiezza = [ 0.5 0.6 0.7]

B = [       
    0.41  6.36  15.44
    0.28  6.37  15.03
    0.25  6.05  14.90
    0.25  6.05  13.50
    0.75  6.05  12.64
    NaN   6.05  12.19
    NaN   5.09  11.81
    NaN   4.45  9.29
    NaN   3.82  9.23
    NaN   3.82  8.82
    NaN   3.50  8.43
    NaN   2.54  8.18
    NaN   2.22  8.15
    NaN   2.72  5.60
    NaN   NaN   6.10
    NaN   NaN   NaN
    NaN   NaN   NaN
                        ]

example:

[Row4, Col1] = 0.25 
I need to replace the NaN in [Row5, Col1] and add 
[Row5, Col1] = [Row4, Col1] + 0.5; 
but 
for i = 6:17 
[Row (i), Col1] = NaN 
I have to do this for all the columns 

[Row13, Col2] = 2.22 

[Row14, Col2] = [Row13, Col2] + 0.6; 

but 
for i = 3:17 
[Row (i), Col1] = NaN 

I have to make this work for a matrix (30,14) and the vector is Ampiezza (1,14)

有帮助吗?

解决方案

This assumes that every column of C contains at least one numeric entry and then at least one NaN (as in your example):

[~, col] = max(isnan(C)); %// find index of first NaN column
ind = col + (0:size(C,2)-1)*size(C,1); %// convert to linear index
B = C; %// initiallize B
B(ind) = B(ind-1) + Ampiezza; %// change entries as desired
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top