MATLAB: How to count number of specific value countnumber I have a matrix and I'm trying to count the number of a specific value in rows (i.e. < 4 ) for example 5 2 3 8 7 1 2 2 3 nnz( A(1,:) < 4); –> 6 but I want sth like this 2 and 4 Best Answer If you have the Image Processing Toolbox, use bwlabel and regionprops:A = [5 2 3 8 7 1 2 2 3][labeledA, numRegions] = bwlabel(A < 4)% Get the size of the regions.props = regionprops(labeledA, 'Area')elementCounts = [props.Area]Run the demo and you'll see this in the command window:A = 5 2 3 8 7 1 2 2 3labeledA = 0 1 1 0 0 2 2 2 2numRegions = 2props = 2×1 struct array with fields: AreaelementCounts = 2 4 Related SolutionsMATLAB: How to get specific value One way:A = [5 2 3 8 7 1 2 2 3]';b = A < 4;ii = bwlabel(b);elementCounts = accumarray(ii(ii>0),1)value = A(b) MATLAB: How can i separate all the connected regions in a binary image and display them individually. Use bwlabel() and ismember[labeledImage, numRegions] = bwlabel(binaryImage);props = regionprops(labeledImage, 'Area')% Compute all the areas.allAreas = [props.Area]% Display each blob in its own figure.for k = 1 : numRegions thisRegion = ismember(labeledImage, k); figure; imshow(thisRegion); drawnow;end Related QuestionHow to fit a series of datapoints using 3 piecewise linear fit linesFind the duration of speechCount the number of times a value occurs in a specific of an arrayHow to assign values of each sub matrix from main matriix into another matrix
Best Answer