필터 지우기
필터 지우기

can this loop be vectorized ?

조회 수: 3 (최근 30일)
Miguel Reina
Miguel Reina 2017년 11월 27일
댓글: Guillaume 2017년 11월 27일
I am trying to make a function for an histogram to explain the concept to students, is possible to vectorize this loop?
[x,y]=size(A);
freq=zeros(256,1);
for i=1:x
for j=1:y
value=A(i,j);
freq(value+1)=freq(value+1)+1;
end
end
thanks in advance.

채택된 답변

Guillaume
Guillaume 2017년 11월 27일
Very simply with:
freq = accumarray(A(:), 1, [256, 1]);
  댓글 수: 2
Miguel Reina
Miguel Reina 2017년 11월 27일
This error is appearing "Error using accumarray First input SUBS must contain positive integer subscripts." the Matrix A contains uint8 values and the size is 594x671
Guillaume
Guillaume 2017년 11월 27일
Yes, somehow I missed the +1 in your code. The correct answer should be:
freq = accumarray(A(:) + 1, 1, [256, 1]);

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by