0

I have an N x N matrix, A, and a vector of row indices, v. I want to replace the diagonal elements of A only for the rows in A specified by v without using a for loop.

For example:

N = 10; A = rand(N,N); %Random N x N matrix v = [1 4 6 9 10]; %vector of row indices %What I want to do but without a for loop: for i = 1:length(v) A(v(i),v(i)) = 0; end %I thought this would work, but it does not: %A(v,v) = 0; 

I feel like there must a one-line method of doing this, but can't seem to figure out what it would be.

Cheers

1 Answer 1

5

Use sub2ind:

A(sub2ind(size(A),v,v)) = 0; 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.