Complete matrix with vectors as indices in matlab

前端 未结 2 466
梦毁少年i
梦毁少年i 2021-01-27 00:47

Let us say that we have a matrix A1 and two vectors v1 and v2 as follow:

A1=zeros(5, 5);
v1=[1 2 3];
v2=[5 5 4];

Is there a way to replace the

2条回答
  •  旧巷少年郎
    2021-01-27 01:39

    Convert your vectors into linear indices:

    A1=zeros(5, 5);
    v1=[1 2 3];
    v2=[5 5 4];
    
    ind=sub2ind(size(A1), v1, v2);
    A1(ind(1))=1
    
    A1 =
    
         0     0     0     0     1
         0     0     0     0     0
         0     0     0     0     0
         0     0     0     0     0
         0     0     0     0     0
    

    etc.

提交回复
热议问题