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

1 visualización (últimos 30 días)
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...]

Respuesta aceptada

Walter Roberson
Walter Roberson el 15 de Jun. de 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) );

Más respuestas (0)

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Productos


Versión

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by