if i have a vector A = [1 1 1 1 2 3 3 5 5 5 5 6 6 7]... its alreay been sortecand only increases.. i want to create a 2nd vector based off A say called B that would have the count of each unique values in the A vector... so, B = [3 1 2 3 2 1] .. because there are 3 1's and 1 2 and 2 3's, etc etc. i just cant seem to get the suntax correct... please help.

 채택된 답변

Star Strider
Star Strider 2021년 5월 9일

0 개 추천

Try this —
A = [1 1 1 1 2 3 3 5 5 5 5 6 6 7];
[Au,~,ix] = unique(A,'stable');
Count = accumarray(ix,1);
Result = table(Au(:), Count(:), 'VariableNames',{'Unique_Elements','Count'})
Result = 6×2 table
Unique_Elements Count _______________ _____ 1 4 2 1 3 2 5 4 6 2 7 1
.

댓글 수: 4

James Baker
James Baker 2021년 5월 9일
haha... my matlab skills are weak... im trying to decode ur answer ;-)
Star Strider
Star Strider 2021년 5월 9일
Likely the only function that may be a bit mysterious is accumarray. Here, it looops through the elements of the vector and counts the occurrences of each one, assigned by the ‘ix’ index variable created by the unique function. (The accumarray function can do a number of other things, depending on how the fourth argument, that can be a number of different functions, accumulates and stores the results. The default fourth argument is simply to sum the occurrences, as it does here. It is worthwhile to experiment with accumarray to understand everything it can do.)
James Baker
James Baker 2021년 5월 9일
ok.. perfect.. after looking at it for a few... just what i needed thanks
Star Strider
Star Strider 2021년 5월 9일
As always, my pleasure!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

제품

릴리스

R2021a

질문:

2021년 5월 9일

댓글:

2021년 5월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by