how to count the number of occurrences of these numbers (with some conditions)
    7 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    cgo
 el 23 de Abr. de 2021
  
    
    
    
    
    Respondida: Bruno Luong
      
      
 el 23 de Abr. de 2021
            I have A(1,:) = [1, 2, 5, 2 , 1 , 2]; 
A(2,:) = [0,1,1,0,1,0]; 
I want to count the unique number of occurences in A(1,:) so this is simply unique(A(1,:)), which will give
[1, 2, 5]
But i want to count/add the number of ones in A(1,:). So I want another output to be this: first element corresponds to the number of 1's for 1. Next, number of 1's for 2, and so on. 
[1,1, 1];
How do i do this?
0 comentarios
Respuesta aceptada
  Bruno Luong
      
      
 el 23 de Abr. de 2021
        A(1,:) = [1, 2, 5, 2 , 1 , 2];
A(2,:) = [0,1,1,0,1,0];
[B,~,J] = unique(A(1,:));
N = accumarray(J(:),A(2,:)');
B = B';
table(B,N)
0 comentarios
Más respuestas (1)
  SungJun Cho
      
 el 23 de Abr. de 2021
        
      Editada: SungJun Cho
      
 el 23 de Abr. de 2021
  
      Hi,
The code below should solve your problem.
A(1,:) = [1,2,5,2,1,2];
A(2,:) = [0,1,1,0,1,0];
uA = unique(A(1,:));
answer = zeros(1,length(uA));
for i = 1:length(uA)
    array = A(2,:);
    answer(1,i) = sum(array(A(1,:) == uA(i)));
end
Here, "answer" will give you the output [1,1,1] and "uA" the array [1,2,5].
0 comentarios
Ver también
Categorías
				Más información sobre Install Products en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!