MATLAB: Negative number in vector MATLABvector say I have a vector v=[1 2 5 6 -8 5 2 7] and I to create a new vector that has all the elements until the negative number shows up such as c=[1 2 5 6] how would you do this without using a while loop Best Answer c=v(1:find(v<0,1,'first')-1) Related SolutionsMATLAB: Finding the number of integers in a vector nnz(v==round(v) & v>0) MATLAB: All negative number on bottom >> 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 QuestionIs vectorizing this even possibleConvert vector position to real numberImproper negative number representationA way to compute dot product of an array and a vector
Best Answer