MATLAB: Selection of 3 large value from a matrix How can i select 3 largest value from a matrix and remaining make zero without changing the dimension,For eg A=[1 3 5 8 31 7 4 18] The output should be A=[0 0 0 8 31 0 0 18] Please help Best Answer A=[1 3 5 8 31 7 4 18][ii,jj]=sort(A,'descend');A(jj(4:end))=0OrA=[1 3 5 8 31 7 4 18][ii,jj]=sort(A,'descend');B=zeros(size(A))B(jj(1:3))=A(jj(1:3)) Related SolutionsMATLAB: What is wrong with the For loop A = input('Enter your matrix: ')for ii = 1:size(A,1) for jj = 1:size(A,2) if A(ii,jj) > 0 A(ii,jj) = 2*(A(ii,jj)); else A(ii,jj) = 3*(A(ii,jj)) ; end endendYou are running loop only along the number of times the row's are. Now I have included columns also.You can achieve the same without loop.B =A ;B(A>0) = 2*A(A>0) ;B(A<0) = 3*A(A<0) ; MATLAB: Modify the matrix, rearrangement of elements The following code might help you:sz = size(a);rowMat = zeros(1,prod(sz));ind = 1;for i = 1:sz(1) j = 1; ii = i; while ii>0 && j<=i rowMat(ind) = a(ii,j); ind = ind+1; j = j+1; ii = ii-1; end if i == sz(1) for j = 2:sz(2) ii = i; jj = j; while ii>1 && jj<=i rowMat(ind) = a(ii,jj); ind = ind+1; jj = jj+1; ii = ii-1; end end endendarowMatRefer to MATLAB Onramp to get started with MATLAB. Related QuestionHow can i find an eleiment which comes only one in a matrix ????How to find arrays in matrix
Best Answer