MATLAB: How to count the followed occurrences of each element in a vector in MATLAB followedoccurrencesvector Hi, I want to count the number of followed occurrences of each element in a vector. So if my input is x = [1 1 1 2 2 1 1 2 5 5] I need an output y = [1 2 1 2 5;3 2 2 1 2] How do I do this? Best Answer Judicious use of diff solves this easily:x = [1 1 1 2 2 1 1 2 5 5];f = [find(diff(x)),numel(x)];y(2,:) = [f(1),diff(f)];y(1,:) = x(f);which generates this output:>> y = 1 2 1 2 5 3 2 2 1 2 Related SolutionsMATLAB: Question about the sign Use sign functionIn your case you can use H(se+1,:)=2*H(se+1,:) MATLAB: Check if an array is equispaced Something like thisx = [1 2 3];dx = diff(x);is_equidistant = all(dx==dx(1));Result>> is_equidistantis_equidistant = logical 1Ifx = [1 2 5];dx = diff(x);is_equidistant = all(dx==dx(1));Result>> is_equidistantis_equidistant = logical 0If you want to check it with a tolerance valuex = [1 2 3.00000000000001];dx = diff(x);is_equidistant = all(ismembertol(dx, dx(1)));Result>> is_equidistantis_equidistant = logical 1 Related QuestionSplit vector every time data changes from 2 to 1Como corrigo el error de los numeros positivos e integros, e intentado con el comando sym…Diff but not when 0How to use matrix elements as indices to access elements of another matrixHow to extract specific row data from a big matrix
Best Answer