Search code examples
arraysmatlabequalitycell-array

MATLAB: Check if a value in a cell array remains the same


My question may be a bit "silly", but I am stucked here, so I would really need your help.

So, we have a cell array Q 5520x1, like the one below:

enter image description here

I'm only interested in the first two numbers of each row. To be specifically clear, giving

K>> Q{1}

ans = 0 1 0 238

I only care about 0 and 1, and so on with the other rows. Only the first two numbers of each row. Let's just ignore the rest values in each row of Q, they are more or less irrelevant.

My problem is, how is it possible to check if the first value of each row in the cell array Q, in the example above 0, remains the same? What I would like to achieve, is while the first value is the same, I want to store in another cell array, all the second values. In case of number 0, I want to store the values

[1,3,127,129,216,217,252,253,302,303,342,343]

and so on. How could this be done?

Any help or advice would be really appreciated.


Solution

  • % Example Q
    Q = {[0 1 135 134];
         [0 3 154 26];
         [0 16 146 234];
         [1 2 324 125];
         [1 7 213 252]};
    A = cell2mat(Q(1:2760));
    [~,~,ind2] = unique(A(:,1));
    C = arrayfun(@(x)A(find(ind2==x),2),1:max(ind2),'UniformOutput',false);
    
    C{1}
    
    ans =
    
         1
         3
        16
    
    C{2}
    
    ans =
    
         2
         7