vector confusion

조회 수: 2 (최근 30일)
Leor Greenberger
Leor Greenberger 2011년 9월 22일
I have a vector A whose values are indices for vector P.
Say A = 2 occurs 10 times.
If I do:
P(A) = P(A) + 1
P(2) = 1 % not 10
I believe I understand why this is happening. Is there a solution without going into FOR loops? A could be quite large.
  댓글 수: 2
the cyclist
the cyclist 2011년 9월 22일
I do not understand what you mean by "A = 2 occurs 10 times". Can you write out code for what you mean?
Leor Greenberger
Leor Greenberger 2011년 9월 22일
A = [1 2 4 5 2 4 6 7 2 ....] <-- 2 occurs 10 times.

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

채택된 답변

Walter Roberson
Walter Roberson 2011년 9월 22일
Especially if you are starting with an all-zero array, use
P = accumarray(A(:),1);
  댓글 수: 1
Leor Greenberger
Leor Greenberger 2011년 9월 22일
excellent!!! I can't believe there is a function for this!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by