0

I am trying to make a new matrix of n rows and 7 columns, but my code only outputs the first column.

%sample values
table_a = [161.0972   11.0000   14.0000    1.0000         0         0         0;
  163.0401    9.0000    8.0000    3.0000         0         0         0;
  163.0765   10.0000   12.0000    2.0000         0         0         0;
  163.1129   11.0000   16.0000    1.0000         0         0         0;
  165.0194    8.0000    6.0000    4.0000         0         0         0;
  165.0558    9.0000   10.0000    3.0000         0         0         0;
  165.0922   10.0000   14.0000    2.0000         0         0         0]
  
table_b = [163.0401    9.0000    8.0000    3.0000         0         0         0;
  163.0765   10.0000   12.0000    2.0000         0         0         0;
  165.0558    9.0000   10.0000    3.0000         0         0         0;
  165.0922   10.0000   14.0000    2.0000         0         0         0;
  167.0350    8.0000    8.0000    4.0000         0         0         0;
  167.0714    9.0000   12.0000    3.0000         0         0         0;
  169.0143    7.0000    6.0000    5.0000         0         0         0]


table_c = table_a(~ismember(table_a(:, 1:7), table_b(:, 1:7)));

This is what I yield:

table_c =
163.0401    
163.0765  
165.0922

This is what I expect to yield:

table_c = 
163.0401    9.0000    8.0000    3.0000         0         0         0
163.0765   10.0000   12.0000    2.0000         0         0         0
165.0922   10.0000   14.0000    2.0000         0         0         0
Rachel
  • 3
  • 1

1 Answers1

1

Your code currently does this:

filter = ~ismember(table_a(:, 1:7), table_b(:, 1:7));
table_c = table_a(filter);

This selects all table_a(x) that have filter(x) = true.

You want to select the rows of table_a that fulfill the condition, but select all columns of those rows. To do this, you have to tell Matlab to select those rows, and all columns of table_a

table_c = table_a(filter, :);

Or in one line,

table_c = table_a(~ismember(table_a(:, 1:7), table_b(:, 1:7)), :);

To clarify, Matlab stores its 2D arrays as column-major arrays. With a matrix

A = [0, 1, 2;
     3, 4, 5;
     6, 7, 8];

A(i) selects the ith element in the column-major-ordered array, so A(4) would give you 1. A(i, j) selects the ith row and jth column of the matrix.

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70