Counting the number of the unique value in each row of a matrix without using for loop
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
reza aghaee
am 30 Jan. 2021
Kommentiert: Stephen23
am 31 Jan. 2021
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 Kommentare
Iuliu Ardelean
am 30 Jan. 2021
Bearbeitet: Iuliu Ardelean
am 30 Jan. 2021
Your solution takes 10.6 seconds for me. One of the solutions in link above took 0.44 seconds.
Stephen23
am 30 Jan. 2021
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...".
Akzeptierte Antwort
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!