MATLAB: Need to remove repeated adjacent elements in an array diff()repeat values removeunique I need to turn [1 1 1 1 2 2 2 6 6 6 6 2 2 2 2] into [1 2 6 2] unique() gives [1 2 6], but I want to preserve the second value any advice? Best Answer Taking advantage of ‘logical indexing’, it is relatively straightforward:A = [1 1 1 1 2 2 2 6 6 6 6 2 2 2 2];B = A(diff([0 A])~=0);The code looks for changes in the differences (from the diff function) in ‘A’, then finds the elements in ‘A’ that correspond to those changes. Related SolutionsMATLAB: How to fit a series of datapoints using 3 piecewise linear fit lines Use this FEXload data.mat % attached aboveydata(end) = []; % your xdata/ydata do not match in length ! slope0 = struct('p', 1, 'x', [min(xdata) max(xdata)], 'v', [0 0]);% Download BSFK function here% https://www.mathworks.com/matlabcentral/fileexchange/25872-free-knot-spline-approximationpp = BSFK(xdata,ydata, 2, 3, [], struct('KnotRemoval', 'none', 'pntcon', slope0)); % Checkfigurexi = linspace(min(xdata),max(xdata));yi = ppval(pp,xi)plot(xdata,ydata,'b',xi,yi,'r');for xb=pp.breaks xline(xb);endgrid on MATLAB: Matrix symbolic calculation doesn’t simplify expressions try simplify - here is an example with your expression:syms wE = eye(3) * - (2^(1/2)*((2^(1/2)*w*1i)/2 - 2^(1/2)/8))/2 + (2^(1/2)*((2^(1/2)*w*1i)/2 + 2^(1/2)/8))/2Simple_E = simplify(E)gives:E = [ - (2^(1/2)*((2^(1/2)*w*1i)/2 - 2^(1/2)/8))/2 + (2^(1/2)*((2^(1/2)*w*1i)/2 + 2^(1/2)/8))/2, (2^(1/2)*((2^(1/2)*w*1i)/2 + 2^(1/2)/8))/2, (2^(1/2)*((2^(1/2)*w*1i)/2 + 2^(1/2)/8))/2][ (2^(1/2)*((2^(1/2)*w*1i)/2 + 2^(1/2)/8))/2, - (2^(1/2)*((2^(1/2)*w*1i)/2 - 2^(1/2)/8))/2 + (2^(1/2)*((2^(1/2)*w*1i)/2 + 2^(1/2)/8))/2, (2^(1/2)*((2^(1/2)*w*1i)/2 + 2^(1/2)/8))/2][ (2^(1/2)*((2^(1/2)*w*1i)/2 + 2^(1/2)/8))/2, (2^(1/2)*((2^(1/2)*w*1i)/2 + 2^(1/2)/8))/2, - (2^(1/2)*((2^(1/2)*w*1i)/2 - 2^(1/2)/8))/2 + (2^(1/2)*((2^(1/2)*w*1i)/2 + 2^(1/2)/8))/2] Simple_E = [ 1/4, (w*1i)/2 + 1/8, (w*1i)/2 + 1/8][ (w*1i)/2 + 1/8, 1/4, (w*1i)/2 + 1/8][ (w*1i)/2 + 1/8, (w*1i)/2 + 1/8, 1/4] Related QuestionRemoving duplicates in an arrayHow to multiply two different length vectors using for loops in MatlabCreate a new array from an intersection of two previous arraysHow to group an array and then replace each group with different numbers
Best Answer