MATLAB Answers

How do i copy a one matrix into another ignoring the ''NAN' elements in the matrix?

2 views (last 30 days)
sachin narain
sachin narain on 7 Jun 2018
Commented: sachin narain on 7 Jun 2018

I have a cell array "raw_Grounds" which is constructed in such a way that for some rows the entire row values from the 2nd column to the end would be 'NaN' i.e. for example in the 3rd row, raw_Grounds(3,2:end) would completly be 'NaN' (as can be seen from the code snippet below.)

i have a 2nd cell array "dataArr". The sizes of "dataArr" and "raw_Grounds" are related such that if the size of "raw_Grounds" is 7x5 (mXn) in which two rows have 'NaN' values as mentioned above, the size of "dataArr" would be 5x4. i.e size of "dataArr" is [(m-no.of NaN row)x(n-1)]

Now, i have to create a 3rd cell array "newRaw_Grounds" by copying the data from "dataArr" to "raw_Grounds" such that the data from "dataArr" fill the non 'NaN' rows of "raw_Grounds".

For eg: if the 3rd and the 5th row of "raw_Grounds"(7x5) have 'NaN' values from (3,2:end) and (5,2:end) respectively, the data from the 1st and 2nd row of "dataArr" should fill the (1,2:end) and (2,2:end) of "newRaw_Grounds", (4,2:end) of "newRaw_Grounds" should be filled with the data from the 3rd row of "dataArr" and (6,2:end) and (7,2:end) row of "newRaw_Grounds" should be filled with the 4th and 5th row values of "dataArr". The values in (3,2:end) and (5,2:end) of "newRaw_Grounds" should be 'NaN' just like in "raw_Grounds "

The first column of "newRaw_Grounds" the same as teh first column of "raw_Grounds".

THe problem is a bit complicated like you can see, but any help on this would be of real help for me. I have attached 3 sample matrices below for better comprehension of the scenario.

Thanks in advance,

   raw_Grounds = {
    ['sfsdfgdsfgdsgdsfg']    '0.556'        '0.455'         '0.235'      '0.5789'
    ['sfsdfgdsfgdsgdsfg']    [      201]    [       3011]    [    40111]    [          50311]    
    ['sfsdfgdsfgdsgdsfg']    [    NaN]    [     NaN]    [  NaN]    [        NaN]
    ['sfsdfgdsfgdsgdsfg']    '0.5'        '0.5'         '0.5'      '0.5'
    ['sfsdfgdsfgdsgdsfg']    [    NaN]    [     NaN]    [  NaN]    [        NaN]
    ['sfsdfgdsfgdsgdsfg']    [      1]    [       1]    [    1]    [          1]
    ['sfsdfgdsfgdsgdsfg']    'Asphalt'    'Concrete'    'Grass'    'Cobblestone'};
data = {
    '0.0515'        '0.4546'         '0.5ez'      '0.5gf'
    '7891'          '2650'          '1874'        '1369fd'
    '0.5'        '0.6'         '0.5'      '0.75'
    '1'          '20'          '1'        '100'
    'Asphalt'    'Asphalt'    'Asphalt'    'Asphalt'};
newRaw_Grounds ={
      ['sfsdfgdsfgdsgdsfg']     '0.0515'        '0.4546'         '0.5ez'      '0.5gf'
      ['sfsdfgdsfgdsgdsfg']    '7891'          '2650'          '1874'        '1369fd'
      ['sfsdfgdsfgdsgdsfg']    [    NaN]    [     NaN]    [  NaN]    [        NaN]
      ['sfsdfgdsfgdsgdsfg']    '0.5'        '0.6'         '0.5'      '0.75'
      ['sfsdfgdsfgdsgdsfg']    [    NaN]    [     NaN]    [  NaN]    [        NaN]
      ['sfsdfgdsfgdsgdsfg']    '1'          '20'          '1'        '100'
      ['sfsdfgdsfgdsgdsfg']    'Asphalt'    'Asphalt'    'Asphalt'    'Asphalt'};


KSSV on 7 Jun 2018
Don't attache code./ data as a image snippet......copy the code here or attach it have the options.

Sign in to comment.

Answers (1)

Nithin Banka
Nithin Banka on 7 Jun 2018
if 'a' is your 1st matrix, 'b' is your 2nd matrix,
a = [17 24 1 8 15;
23 5 7 14 16;
4 NaN 13 20 22;
10 12 19 21 3;
11 18 25 NaN 9];
nanIdx = isnan(a);
a(~any(nanIdx, 2), :) = b(~any(nanIdx, 2), :);
This will copy all the rows in 'b' corresponding to non-NaN rows in 'a' to 'a'

  1 Comment

sachin narain
sachin narain on 7 Jun 2018
Hi, Thank you very much for your response, but this method wouldn't work for me. I forgot to mention the fact that the 2 matrices would be of different sizes. I have edited the post for better comprehension. Kindly review the post

Sign in to comment.

Sign in to answer this question.