MATLAB: Extracting zeros from vectors extracting I need to extract zeros from a vector while keeping the same order of elements in s. Like this : S=[1 0 0 2 3 0 0 0 2 1 0 4 0 0 0 0 1 0 2] S1=[1 2 3 2 1 4 1 2] Best Answer >> S=[1 0 0 2 3 0 0 0 2 1 0 4 0 0 0 0 1 0 2] S =nonzeros(S)'S = Columns 1 through 13 1 0 0 2 3 0 0 0 2 1 0 4 0 Columns 14 through 19 0 0 0 1 0 2S = 1 2 3 2 1 4 1 2>> Related SolutionsMATLAB: Add a value to an element in a matrix Cause is a specific problem where I need to add that values of S just in these columns of AThen, this should work:A(S(1, :) ~= 0, 6) = nonzeros(S(1, :));A(S(2, :) ~= 0, 2) = nonzeros(S(2, :));Of course, if there were more rows in S a completely different approach would be better, but for just 2 rows this will do. MATLAB: Removing zeros from matrix A = A.'; A = reshape(A(A~=0),3,4).'; Related QuestionExcluding one vector from another vector with repetitionIs it possible to use a function in a vectorizationMatlab multiple segments extractionAdding elements to beginning of each rowDelete iterations in a string of numbersHelp to find similar variables and add them togeherReplace values in matrix by first non-zero value in previous row
Best Answer