MATLAB: Reversing Columns in a Matrix matrix manipulation I need to reverse the order of specific columns in a vector. For example: a = [1 2 3 4 5 6] Reverse columns 2:5 a = [1 5 4 3 2 6] How do I do this? Best Answer Use fliplr to flip a matrix left-to-right:a = [1 2 3 4 5 6]b = [a(1) fliplr(a(2:5)) a(6)]See also: flipud (flips it up-to-down) or flipdim (flips along a specified dimension).(Edit based on your comments below)So if you want to flip every permutation of elements (2:3, 2:4, 2:5, 2:6, 3:4, etc), then you can also use nchoosek as follows:a = [1 2 3 4 5 6];twoInds = nchoosek(1:length(a), 2);numPerms = size(twoInds,1);all_A = repmat(a, numPerms, 1);for i = 1:numPerms colNos = twoInds(i,1):twoInds(i,2); all_A(i, colNos) = flipdim(a(colNos),2);endSo now you have your original "a" variable, plus a matrix "all_A", where every row has a different piece of "a" flipped, left to right.Was that what you were looking for? Related SolutionsMATLAB: A simple matrix question A = [1 2 3 4 5];B = fliplr(A) % Flip matrix in left/right directionA = [1; 2; 3; 4; 5];B = flipud(A) % Flip matrix in up/down direction MATLAB: How to swapping values in matrix row a=[1 6 6 89 5 7 2];a(2,:) = fliplr(a(2,:)); Related QuestionAll the combinations of sums of rows of matricesHow to eliminate rows?This question is about making a square matrix of order n x n in which the integers from 1 to ? 2 wind back and forth along the rows. If n=3 then [1,2,3;6,5,4;7;8;9]Is there a better to multiply matrices with is patternI want to traverse an array in an ‘inverse s’ order and store in a single row array is there any matlab function or code for this?eg[1 2 3; 3 4 5; 4 5 1] becomes [1,2,3,5,4,3,4,5,1]
Best Answer