MATLAB: All negative number on bottom matrixsort How i can sort matrix of numbers and put negative on bottom respecting the order of positive elements. A = [1,-3,4;-2,5,6;4,2,1]newA = [1,5,4;4,2,6;-2,-3,1] Best Answer >> A = [1,-3,4;-2,5,6;4,2,1]A = 1 -3 4 -2 5 6 4 2 1>> S = size(A);>> [~,R] = sort(A<0,1);>> C = ones(S(1),1)*(1:S(2));>> X = sub2ind(S,R,C);>> A(X)ans = 1 5 4 4 2 6 -2 -3 1 Related SolutionsMATLAB: How to add value at end of row in a matrix Without any loops:R = [2;4;5;2];C = [1;3;6;7];[~,X] = sort(R);[~,~,Y] = unique(R);Z = accumarray(Y,ones(size(R)),[],@(v){v});Y(X) = cell2mat(cellfun(@cumsum,Z,'UniformOutput',false));A = zeros(max(R),max(Y));A(sub2ind(size(A),R,Y)) = Ccreates this output:A = 0 0 1 7 0 0 3 0 6 0 MATLAB: Hello, how can i remove zero from an array and leave the array in the same form? thanks Edit 20170214: added comments as requested>> A = [1,0,2,6,0;0,3,4,0,0;5,0,2,7,4]A = 1 0 2 6 0 0 3 4 0 0 5 0 2 7 4>> S = size(A);% sort the zero elements of A to be last, C contains the column indices of the sorted data:>> [~,C] = sort(A==0,2);% define row indices:>> R = repmat((1:S(1)).',1,S(2));% use the row and (sorted) column indices to create linear indices, sort A using the linear indices:>> B = A(sub2ind(S,R,C));% remove all columns that contain only zeros:>> B(:,all(B==0,1)) = [];% replace zeros with NaN:>> B(B==0) = NaNB = 1 2 6 NaN 3 4 NaN NaN 5 2 7 4 Related QuestionHow to assign values of each sub matrix from main matriix into another matrixAbout maximum value in matrixHow to find a minimum in this matrixHow to fix the code to produce ones along the reverse diagonalHow to sort out negative and positive values of a vectorHello…….i have a matrix…….what i wonaa do is count the repeated entries in column 2 first then make arrangement =(number of repeated termes)! ..for example in given matrix i have two repeated terms so first arrangmnt..1 2 3 the second 2 1 3How to modify a matrix to change the entries >=0 and multiply it by 5 AND to change the entries <0 and add 2 to those entries
Best Answer