Info
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
How to optimize this loop
조회 수: 2 (최근 30일)
이전 댓글 표시
I want to do a loop like this
for i = 1:n
A(i,B(i,1)) = 1;
end
but my matrix has size one million. Is there a way to do it faster?
댓글 수: 0
답변 (2개)
Michelangelo Ricciulli
2017년 8월 16일
I think that this should save some time, since it avoids the for-loop:
i = 1:n;
A(i,B(i,1)) = 1;
댓글 수: 1
Image Analyst
2017년 8월 16일
On my computer, it takes only 9 milliseconds for a million elements:
rows = 1000000;
columns = 5;
A = rand(rows, columns);
B = randi(columns, rows, 1);
tic
for k = 1 : rows
A(k, B(k)) = 1;
end
toc
Why do you need it faster?
댓글 수: 1
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!