Creating a matrix with 2 vectors
2 views (last 30 days)
Show older comments
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
0 Comments
Answers (1)
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;
See Also
Categories
Find more on Multidimensional Arrays in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!