필터 지우기
필터 지우기

How to convert A= [13;8;12;4​;8;1;13;0;​10;13;1;6;​14;13;1;14​;11;9;9;10​;10;15] to B= [3 0 0 1 0 1 0 2 2 3 1 1 4 2 1]

조회 수: 7 (최근 30일)
if A= [13;8;12;4;8;1;13;0;10;13;1;6;14;13;1;14;11;9;9;10;10;15]
how can I convert A to be: B= [3 0 0 1 0 1 0 2 2 3 1 1 4 2 1].
3 in the first index of B, because "1" is repeated three times in A.
2 in the eighth index of B, because "8" is repeated two times in A.
"0" in the second index of B, because the value 2 is not exist in A and so on...

답변 (2개)

Simon Chan
Simon Chan 2021년 7월 8일
edges = 1:max(A)+1;
B = histcounts(A,edges)

Jonas
Jonas 2021년 7월 8일
편집: Jonas 2021년 7월 8일
use histcounts with integer bin method
histcounts([13;8;12;4;8;1;13;0;10;13;1;6;14;13;1;14;11;9;9;10;10;15],'BinMethod','integers')
you forgot the number 0 in your example output

카테고리

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

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by