Construct this matrix based on two vectors MATLAB

前端 未结 3 365
花落未央
花落未央 2021-01-22 02:22

I do have 2 vectors and i want to construct a matrix based onr and c

r =

 1
 2
 4
 6
 8

c =

 2
 4
 6
 8
10
         


        
3条回答
  •  猫巷女王i
    2021-01-22 02:51

    You could use linear indexing to accomplish this.

    First, construct a matrix made out of zeros:

    A = zeros(max(r),max(c));
    

    Then set the elements to 1:

    A( size(A,1) * (c-1) + r ) = 1;
    

提交回复
热议问题