jessems
jessems

Reputation: 1592

How to use double indexing to redefine matrix values

Say I have a matrix A:

A = [1 2; 3 4];

I can use double indexing to retrieve, say, the values exceeding 3 in the third row:

>> B = A(2,:)((A(2,:)>3))
>> B = 4

However, using double indexing to redefine matrix values doesn't work:

>> A(2,:)((A(2,:)>3)) = 0
>> error: () must be followed by . or close the index chain

How can I accomplish this without putting A(2,:) into a variable, performing the operation and putting it back into A(2,:) again?

Upvotes: 1

Views: 1785

Answers (1)

jessems
jessems

Reputation: 1592

I found the answer literally 1 second after posting. The key is to not use double indexing.

>> A(2,A(2,:)>3) = 0

Upvotes: 3

Related Questions