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