Search code examples
matlabcell-array

Table comparison cell content reference from non cell array object


I have two tables with same variables. One table contain one row while other table contain more than one rows.

    a=[1 2; 2 3],b=[2 3; 1 2]
     S1=table(a,b)
    a=[1 1],b=[1 1]
    S2=table(a,b)
    if all(S2{:,:}<S1{:,:}) & any(S2{:,:}<=S1{:,:})
    S1=[S1;S2]
    end

Where is the mistake in specifying table or cell? Even the conversion table2cell, table2struct, table2array did not work (getvar error was shown).

Table values are fixed. There is no addition, no replacement, but the appending only when the condition is satisfied. Final output is table with values as shown.

S1 = 3×2 table 
  a        b
______ ______ 
 1 2       2 3
 2 3       1 2 
 1 1       1 1

Solution

  • The error is due to the fact that you are trying to compare two set of data (S1 and S2) that have different size.

    S2{:,:}
         1     1     1     1
    S1{:,:}
         1     2     2     3
         2     3     1     2
    

    If you want to compare each row of S1 against S2 you can use the function bsxfun:

    to check S2 < S1

    bsxfun(@lt,S2{:,:},S1{:,:})
    

    to check S2 <= S1

    bsxfun(@le,S2{:,:},S1{:,:})
    

    This will lead to:

    if all(bsxfun(@lt,S2{:,:},S1{:,:})) & any(bsxfun(@le,S2{:,:},S1{:,:}))
       S1=[S1;S2]
    end