If I have a list of numbers how can I enter a 1 in a matrix position with the list of numbers

1 view (last 30 days)
My list of numbers is 1690 numbers long with random numbers
list = [ 1, 2, 3, 4, 6, 7, 9...]
My initial matrix is zeros
initmat = zeros(2387,1);
My goal is to get the number 1 into the correct position in the initial matrix from the list (for instance a 1 in the first, second, fourth, sixth, ninth position etc).
I want my final matrix to look like finalmat, but I the list is too long to do by hand
finalmat = [1, 1, 1, 1, 0, 1, 1, 0, 1...]

Accepted Answer

Walter Roberson
Walter Roberson on 15 Jun 2018
initmat(list) = 1;
Another way of handling this as a single step is
finalmat = accumarray(list(:), 1);
or
finalmat = full( sparse(list, 1, 1) );

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!