필터 지우기
필터 지우기

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에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by