Thank you for those who tried to answer my question.
How to get all values existing in arrays/matrices?
77 ビュー (過去 30 日間)
古いコメントを表示
Hello,
Let say I have a 3-by-2 array with random integer values. For example,
myArray = [98 56; 58 52; 100 56];
What I want to do is, I want to get all value in the array and its frequency and save it to another array like this,
listValue = [98 1; 56 2; 58 1; 52 1; 100 1];
Is there any function or a way to do it?
Thank you in advance.
採用された回答
Andrei Bobrov
2020 年 3 月 24 日
編集済み: Andrei Bobrov
2020 年 3 月 24 日
[a,~,c] = unique(reshape(myArray',[],1),'stable');
out = [a, accumarray(c,1)];
or
out = varfun(@(x)x,array2table(myArray(:)),'GroupingVariables',1);
2 件のコメント
Andrei Bobrov
2020 年 3 月 24 日
編集済み: Andrei Bobrov
2020 年 3 月 24 日
Yes or:
[a,~,c] = unique(myArray);
out = [a, accumarray(c,1)];
その他の回答 (2 件)
Walter Roberson
2020 年 3 月 24 日
See unique() and accumarray. Or unique and histc or histcounts (but be careful about the last value in histcounts). Or use a loop. Or use sparse(). Lots of ways.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!