1

Let's say that i've a 3 columns matrix like this one:

1   2   0,1 "A"
1   3   0,2 "B"
1   4   0,3 "C"
1   5   0,4
1   6   0,5
1   7   0,6
1   8   0,7
1   9   0,8
1   10  0,9
1   11  1
2   3   1,1 "A"
2   4   1,2 "B"
2   5   1,3 "C"
2   6   1,4
2   7   1,5
2   8   1,6
2   9   1,7
2   10  1,8
2   11  1,9
3   4   2   "A"
3   5   2,1 "B"
3   6   2,2 "C"
3   7   2,3
3   8   2,4
3   9   2,5
3   10  2,6
3   11  2,7
4   5   2,8 "A"
4   6   2,9 "B"
4   7   3   "C"
4   8   3,1
4   9   3,2
4   10  3,3
4   11  3,4
5   6   3,5 "A"
5   7   3,6 "B"
5   8   3,7 "C"
5   9   3,8
5   10  3,9
5   11  4
6   7   4,1 "A"
6   8   4,2 "B"
6   9   4,3 "C"
6   10  4,4
6   11  4,5
7   8   4,6 "A"
7   9   4,7 "B"
7   10  4,8 "C"
7   11  4,9
8   9   5   "A"
8   10  5,1 "B"
8   11  5,2 "C"
9   10  5,3 "A"
9   11  5,4 "B"
10  11  5,5 "A"

How can I extract in different matrices all the rows labeled with "A"? the steps is 10, 9,..,2 The same thing should be done for the rows labeled with "B" and then with "C" and so on. The output should be:

    1   2   0,1                 
    2   3   1,1         1   3   0,2
    3   4   2           2   4   1,2
    4   5   2,8         3   5   2,1
A = 5   6   3,5         4   6   2,9
    6   7   4,1     B = 5   7   3,6
    7   8   4,6         6   8   4,2
    8   9   5           7   9   4,7
    9   10  5,3         8   10  5,1
    10  11  5,5         9   11  5,4

NOTE: The 3-columns matrix is the output of multcompare where the first two columns are the comparisons between an 11-columns matrix.

Divakar
  • 218,885
  • 19
  • 262
  • 358
gmeroni
  • 571
  • 4
  • 16
  • What exactly is that `10, 9,..,2 ` thing? – Divakar Jan 27 '15 at 13:24
  • 1
    I can not see a matrix, did you maybe mean a [cell](http://se.mathworks.com/help/matlab/ref/cell.html)? And in that case what is the size of the cell? And the elements inside each cell (which may be matrices or vectors). – patrik Jan 27 '15 at 13:24
  • @Divakar is the number of the steps between the "A" labeled row. – gmeroni Jan 27 '15 at 13:29
  • @patrik forget about "A", "B" and "C", those labels are just to explain the problem here, in matlab it's just a 3-column matrix (no labels) – gmeroni Jan 27 '15 at 13:29

2 Answers2

2

Since all rows labeled with A are always x and x+1 in the first two columns, you can use:

A = Z(find(Z(:,1)==Z(:,2)-1),:)

For B the difference is two, thus,

B = Z(find(Z(:,1)==Z(:,2)-2),:)

and so on, assuming Z is your initial data.

Nemesis
  • 2,324
  • 13
  • 23
2

Assuming M to be the input array, you can use the pattern of decaying offsets [10 9 8 7 6 5 ...] between the consecutive row IDs of A alongwith cumsum to get the actual row IDs in M. Then, use matrix-indexing to get the corresponding rows of M for the final outputs A, B and C -

offsets = 10:-1:2 %// offsets between labels
ids = cumsum([1 offsets]) %// obtain actual row ids for A

A = M(ids,:) %// get the corresponding rows of M for A by directly using ids

%// Get the corresponding rows of M for B and C by using ids and adding 1 and 2
%// respectively as B and C are at offsets 1 and 2 with respect to A and 
%// number of such row ids are one less for B and two less for C 
B = M(ids(1:end-1)+1,:) 
C = M(ids(1:end-2)+2,:)
Divakar
  • 218,885
  • 19
  • 262
  • 358