Zanam
Zanam

Reputation: 4807

vector of indices

I have a 10x10 matrix called A:

I have vector of column numbers:

C = [ 2, 6, 8 ];

I have a vector of row numbers:

R = [1; 3; 7];

The column numbers correspond to each row. i.e. For column 1 we are looking at row numbers given by R, for column 3 we are looking at row numbers given by R and so on.

I want to replace those exact locations in A with some other number 13.

i.e. for each of these locations in matrix A:

(1,2) (1,6) (1,8), (3,2), (3, 6), (3,8) I want to insert 13.

How do I achieve the above ?

Upvotes: 1

Views: 181

Answers (2)

Trenera
Trenera

Reputation: 1505

As dlavila pointed out, you can do A(R,C) = 13 wich would be the best and easiest. Nevertheless I have written a longer code involving the eval function that you might find useful in the future:

for ii=1:length(C)
    for jj =1:length(R)
        eval(strcat('A(', num2str(C(ii)), ',',num2str(R(jj)),')=13;'))
    end
end

Both give the same results.

Upvotes: 0

dlavila
dlavila

Reputation: 1212

you can do A(R,C) = 13 .......

Upvotes: 4

Related Questions