Search code examples
matlabcell-array

How to perform logical indexing of cell array for sparse matrix in coordinate format?


Let A be a sparse matrix in coordinate format [row(int) col(int) val(float)]. If a upper triangular sparse matrix of A is needed, then the same can be obtained using logical indexing like:

A = A(A(:,1) <= A(:,2), :);

If A is cell array [{row(int)} {col(int)} {val(string)}], how do I perform the same logical indexing as above in this case?


Solution

  • You can use cell2mat to transform a column of the cell into a matrix that can be used as an index list:

    A={1,2,'top';2,1,'bottom'}
    A = 
        [1]    [2]    'top'   
        [2]    [1]    'bottom'
    >> A(cell2mat(A(:,1))<=cell2mat(A(:,2)),:)
    ans = 
        [1]    [2]    'top'