MATLAB: How to do repeating numbers in array matlab functionscript Hi I have an array like [1 0 0 0 2 0 0 0 3 0 0 0 0 0 5 0 0 0 0]. I want output like, [1 1 1 1 2 2 2 2 3 3 3 3 3 3 5 5 5 5 5]. how i can do this? Thanks in Advance!!! Murugan Best Answer An old fashioned way:>> V = [1,0,0,0,2,0,0,0,3,0,0,0,0,0,5,0,0,0,0];>> X = V~=0;>> U = V(X);>> U(cumsum(X))ans = 1 1 1 1 2 2 2 2 3 3 3 3 3 3 5 5 5 5 5 Related SolutionsMATLAB: Add blank spaces in cell array ar = [0,1,0,0,0,0,0,0,0,0; ... 0,0,0,3,0,0,0,0,0,0; ... 0,0,0,0,0,0,2,0,0,0; ... 0,0,0,0,0,0,0,0,3,0; ... 4,0,0,0,0,0,0,0,0,0];fmt = repmat('%11d', 1, size(ar,2));% Either:C = sprintfc(fmt, ar); % Unfortunately not documented% Or:D = compose(fmt, ar) % Working since R2016b MATLAB: Repeating elements (vector) There are most likely much more efficient algorithms than this:>> N = 4;>> X = (1:N)'*ones(1,N);>> X(triu(true(N)))ans = 1 1 2 1 2 3 1 2 3 4You can use X as indices into your vector to select the values that you want. Related QuestionHow to find elements in an array based on a search from another array.Test different number(values or label) in matrixRepeating numbers in matrix3D matrix summation
Best Answer