MATLAB: Finding rows in a matrix count I have a matrix A = [1 2; 2 1; 1 2; 2 2; 1 1; 2 2] I want to count how many times the row [1 2] appears in above matrix A. Here for my counting purpose [1 2] would appear 3 times as [1 2] or [2 1]. Thanks guys Best Answer Another:% The given matrixA = [1 2; 2 1; 1 2; 2 2; 1 1; 2 2];% Now find the counts.[I,J,K] = unique(sort(A,2),'rows'); % I has the unique rows.C = histc(K,1:max(K)); % This has the corresponding counts.% Now that we have found the counts, display them:fprintf('Row [%i %i] appears %i times. \n',[I C]')If you want to only get the counts for the one type, this will do it quickly:cnt = sum(all(bsxfun(@eq,sort(A,2),[1,2]),2)); Related SolutionsMATLAB: Split vector every time data changes from 2 to 1 vectors = mat2cell(A, diff([0;find(A(1:end-1)==2 & A(2:end) == 1); length(A)]),1); MATLAB: Matrix Manipulation A = [1; 1; 3; 1; 2; 1; 1; 1; 3; 1; 1; 1; 1; 2; 1; 1; 1; 1; 1; 3; 1; 1; 1; 2; 1; 1; 1; 1; 1; 2; 2; 1; 1; 1; 2; 1; 1; 2; 1; 1; 1; 1; 1; 1; 3; 1; 1; 3; 2; 1; 1; 1; 1; 1; 1; 1; 2; 2; 4; 1; 1; 2; 1; 1; 1; 1; 2; 2; 1; 1; 1; 3];a1=find(A==2)-1; %find the index of the 2a2=find(A==3)-1; %find the index of the 3a2=repmat(a2,2,1) %duplicate the 3 indexesB=sort([a1;a2])'; %join and sort the solution, also transpose%I transposed B because of your B example Related QuestionHow I find rows with same elements but in flipped manner.How to calculate number of unique element in arrayHow to count the number of times a pair of values occurs in an N by 2 matrixRemove rows/columns with same elementsHow to find a specific row in a matrix and eliminate it
Best Answer