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

1 次查看(过去 30 天)
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...]

采纳的回答

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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by