Search code examples
matlabmatrixindexingadjacency-matrix

Accessing two matrices simultaneously in MATLAB


I have two matrices, one of them is an adjacency matrix : *Red indicates the index associated

another matrix is storing indices of above matrix like this : *Red indicates the index associated

I want to apply some operations on say the 1st row of the matrix (i.e. [1 5 7]) simultaneously, how to access only 1st, 5th, and 7th row of original adjacency matrix in MATLAB?


Solution

  • You can use an array as the row index into the adjacency matrix and you can either store that in a temporary variable or pass that directly to a function.

    tmp = adjacency([1 5 7], :);
    dothing(tmp)
    
    % Or
    dothing(adjacency([1 5 7], :));
    

    Also you can assign back into those same rows using the array as an index again

    adjacency([1 5 7], :) = dothing(adjacency([1 5 7], :));