MATLAB: Trying to use find on a sparse matrix and doesn’t give correct row index

findindex of nonzero elementsparse matrix

I have a large sparse matrix A, lets say 15700 x 6000. There is one non zero term in element (a,b), lets say 33 is the value. If I try to do
[row_idx, col_idx, value] = find(A(a,:));
it returns
row_idx = 1, col_idx = b, value = 33
So every time it says the row_idx is 1 instead of a, and it should be a because I am specifying to look just in that row. I also noticed that if I just call the A(a,b) it says
ans = (1,1) 33
instead of
ans = (a,b) 33
does anyone have any idea why I can't find the correct index value for the row? If I don't specify the row, and just use find on the whole matrix the indices are reported just fine. However, I need to specify the row to search in for something very specific I need to do. :/

Best Answer

When you do
find(A(a,:))
then A(a,:) is processed, giving a temporary variable that has only one row. Then find() is being applied to that temporary variable, and of course the row number is 1 since there is only one row.
In the same way, A(a,b) is processed, giving a temporary variable that has only one element. Then the default action of displaying the value is applied, and that action displays that element (1,1) [of the temporary value] is where the data is.
Except in some cases when an expression appear on the left side of an assignment statement, expressions are always processed "by value", giving a temporary value as the result. For example,
x = 2:4
5*x
processes the 5*[2,3,4] giving a temporary variable [10,15,20]