MATLAB: How would you calculate the amount of 1 sets in a matrix MATLABmatrix For example if I have a matrix, Mtx1 = [0 0 0 0 1 1 1 1 1 0 0 0 1 1 0 0 0 1 1 1 1 1 1]; How would I get a matrix of the counted ones in a set of ones like below. Mtx2 = [5 2 6]; Best Answer Mtx1 = [0 0 0 0 1 1 1 1 1 0 0 0 1 1 0 0 0 1 1 1 1 1 1];ind01 = strfind([0,Mtx1],[0 1]) - 1;ind10 = strfind([Mtx1,0],[1 0]);ind10 - ind01ans = 5 2 6 Related SolutionsMATLAB: Remove elements from a matrix Very simply using cumprod:>> M = [1,1,1,1,1,1,0,0,0,0,0,0;1,1,1,1,1,1,1,1,0,0,0,1;1,1,1,1,1,0,0,0,1,0,0,1;1,1,1,1,1,1,0,0,0,1,0,1]M = 1 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 0 0 0 1 0 0 1 1 1 1 1 1 1 0 0 0 1 0 1>> cumprod(M,2)ans = 1 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 MATLAB: Do not take any amount pop=randi([100],20,8);[r1,c1]=size(pop);pool = zeros([],c1) ; count = 0 ; for i=1:r1 A=randi(100); if A<=90 count = count+1 ; pool(count,:)=pop(i,:); endendYou need not to use loop...it can be vectorized. pop=randi([100],20,8);[r1,c1]=size(pop); A = randi(100,1,r1) ; pool = pop(A<=90,:) ; Related QuestionWarning: Unsuccessful read: The specified amount of data was not returned within the Timeout period.Concatenate x amount of matricesLogical operation on column of a matrixShift rows by different amounts
Best Answer