1

I have an array X (array of vectors) which is formed by 2 lines and 3 columns.

 a1=[1 2 3]; 
 b1=[2 5 4];
 c1=[2 2 4];

 a2=[1 6 5];
 b2=[1 6 4];
 c2=[4 5 7];

 X= {a1,b1,c1 ; a2,b2,c2};

Suppose that I select the first line (a1, b1 and c1) from the array X.

[m n]=size(X); % m=2 and n=3
selected_line = X(1, 1:n);

How can I rewrite the same X but without the first line? In other words, how can I remove the selected line from my table in order to get the array {a2, b2, c2} instead of X described above?

Andy Clifton
  • 4,926
  • 3
  • 35
  • 47
Christina
  • 903
  • 16
  • 39
  • possible duplicate of [Removing rows and columns from MATLAB matrix quickly](http://stackoverflow.com/questions/4163876/removing-rows-and-columns-from-matlab-matrix-quickly). Also, the related question [How to select a submatrix (not in any particular pattern) in Matlab](http://stackoverflow.com/questions/13091193/how-to-select-a-submatrix-not-in-any-particular-pattern-in-matlab/13093242#13093242) is worth a read... – Eitan T Dec 15 '13 at 09:00

1 Answers1

3

In general, you can remove a row or column in a matrix or cell array, or an entry in a vector, by "assigning " it the empty matrix (see for example here and here). So in your case, to remove first line of cell array X use

X(1,:) = []; %// remove first row

Another possibility is to specify which rows remain, instead of which to remove:

X = X(2:end,:); %// take from row 2 to last
Community
  • 1
  • 1
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
  • 2
    @Parag That probably depends on your Matlab version and the size of array... Better to test for each specific case. In my system, with the data in the original post, the second is a little faster – Luis Mendo Dec 15 '13 at 00:46
  • Thank you for your response :),, is it x(1, :) = [] or x(1,:)= {} ? – Christina Dec 15 '13 at 01:32
  • @Parag Assigning `[]` is usually less efficient, [especially when truncating off the end of an array](http://stackoverflow.com/a/19648283/2778484), which would correspond to removing columns (contiguous memory), but the same inefficiencies apply with removing rows, I would imagine. – chappjc Dec 15 '13 at 02:43
  • @chappjc You have a 24GB RAM computer :P, anyway great answer :) – Autonomous Dec 15 '13 at 03:09