Counting the number of the unique value in each row of a matrix without using for loop

조회 수: 2 (최근 30일)
M = randi(21,[1000000,9]);
counter = zeros(size(A,1),1);
for j = 1 : size(A,1)
a = unique(Mat(j,:));
counter(j) = numel(a);
end
Hi
I have big size matrix (M in the above code) and I want to count the number of the unique value in each row.
I do not need the values and only the number of unique value is important to me.
The above for loop is a part of my program that needs to be repeated more and more times.
Because of the size of M, it is very time consuming and i dont want to use the above for loop.
Is there a faster alternative?
Thanks
  댓글 수: 2
Stephen23
Stephen23 2021년 1월 30일
The reason why I gave up on Stack Exchange was because of too much incorrect information or very poor understanding of how to use MATLAB effectively. This thread is a good example, when the most popular answer starts with the remarkably narrow-minded "You need to use unique function on each row...".

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

채택된 답변

Stephen23
Stephen23 2021년 1월 30일
M = randi(9,5,7)
M = 5×7
5 4 8 4 9 6 9 3 2 5 9 8 1 8 4 4 8 7 3 3 1 6 7 4 6 6 3 1 6 3 4 2 5 9 7
N = 1+sum(diff(sort(M,2),1,2)~=0,2)
N = 5×1
5 6 5 5 7

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by