Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Construct this matrix based on two vectors MATLAB

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

i want to construct a matrix A such that A(1,2)=A(2,4)=A(4,6)=A(6,8)=A(8,10)=1 other elements 0.

please help

like image 926
fadesa Avatar asked Mar 24 '26 19:03

fadesa


2 Answers

You could use the constructor for sparse matrices:

full(sparse(r,c,1))

by the way, if you want to apply this to large matrices with many zeros, stay with the sparse one. It uses much less memory for matrices with many zeros:

sparse(r,c,1)
like image 117
Daniel Avatar answered Mar 27 '26 08:03

Daniel


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;
like image 21
Peter Sorowka Avatar answered Mar 27 '26 10:03

Peter Sorowka