Creating a matrix with 2 vectors

2 views (last 30 days)
Hadar Israeli
Hadar Israeli on 20 Jul 2015
Commented: Hadar Israeli on 20 Jul 2015
Hello, I want to create a matrix of ones and zeros using pairs defined by 2 vectors. Is there a smart way of doing that? For example if I have 2 vectors I=[1,1,2,2,3,3] and J=[1,2,3,4,5,6] I want a matrix that looks like this:
1 1 0 0 0 0
0 0 1 1 0 0
0 0 0 0 1 1
Thank you

Answers (1)

Walter Roberson
Walter Roberson on 20 Jul 2015
A = full(sparse(I,J,1));
or
A = accumarray([I(:), J(:)], 1);
or
A = zeros(max(I),max(J));
A(sub2ind(size(A), I, J)) = 1;

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!