MATLAB: How to find the number of occurances occurances i have a matrix x=[1 1 1 1 2 2 1 1 3 3 ] i want the output to be x1=[1 2 1 3] and y1=[4 2 2 2] x1 should have the reduced number from input and x2 should be the number of ocurances Best Answer x=[1 1 1 1 2 2 1 1 3 3];idx=logical([diff(x)~=0,1]);x1=x(idx)yy=find(idx);yyy=diff(find(idx));y1=[yy(1) yyy]x2=histc(x,unique(x)) % number of occurencesunique_numbers_and_their_occurences=[unique(x); histc(x,unique(x))].' %bonusGives:x1 = 1 2 1 3y1 = 4 2 2 2x2 = 6 2 2unique_numbers_and_their_occurences = 1 6 2 2 3 2 Related SolutionsMATLAB: Permute matrix elements across rows [xx yy zz] = ndgrid(X(:,1),X(:,2),X(:,3)); X3 = sortrows([xx(:), yy(:),zz(:)],[1 2 3]); isequal(X2,X3) % ans = 1 MATLAB: Given matrices XX and YY of sizes 3X3, how can I generate the following matrix: [XX(1,1) YY(1,1); XX(1,2) YY(1,2)… or:XX = magic(3);YY = eye(3);XX_trans = transpose(XX);YY_trans = transpose(YY);out = [XX_trans(:) YY_trans(:)]; Related QuestionCalculate the gradient f=@(x) (x(1)-2)^4+(x(1)-2)^2*x(2)^2+(x(2)+1)^2Plotting a Stacked Bar graph using different X values
Best Answer