How can I convert an Array with row values to a binary Matrix?
조회 수: 3 (최근 30일)
이전 댓글 표시
Hi,
I have an array A containing natural numbers. The indices of the array represent the current column and the numbers itself represent the rows of a matrix M. Now I want to set the matrix at one for the corresponding values in the array.
Like the following code
A = [2 3 3 4 5 4 3];
M = zeros(length(A));
for i= 1:length(A)
M(A(i),i) = 1;
end
M
I know that using loops in Matlab can be very slow and I want a faster way of computing this, since the array in my project has a length of 60000. Is there a way to run compute this faster?
댓글 수: 0
채택된 답변
Cris LaPierre
2023년 6월 30일
A = [2 3 3 4 5 4 3];
M = zeros(length(A));
Col = 1:length(A);
ind = sub2ind(size(M),A,Col);
M(ind) = 1
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!