MATLAB: I have two vectors A and B. Also I have a matrix K. I want to use the vectors A and B as coordinates in K. How to do that

coordinatesvectors

K=zeros(30, 30);
A=[2 3 5 6 8]; B=[5 6 3 7 8];
I want to change some zeros in K to 30. This should happen on the following coordinates: 2,5 3,6 5,3 6,7 8,8 How can I create these coordinates from these two vectors?

Best Answer

K=zeros(30, 30);
A = [2 3 5 6 8];
B = [5 6 3 7 8];
valuesToInsert = rand(5,1);
linearIndex = sub2ind(size(K),A,B);
K(linearIndex) = valuesToInsert;
Related Question