필터 지우기
필터 지우기

Writing code to calculate number of ones & zeros in the logic matrix

조회 수: 1 (최근 30일)
Ali Noori
Ali Noori 2015년 5월 27일
댓글: Ali Noori 2015년 5월 27일
Hi all, I'd like to write code to find number of ones & number of zeros ones respectively in logic matrix (1x34) attached below: I will be so grateful if someone help me. Best Regards

채택된 답변

Walter Roberson
Walter Roberson 2015년 5월 27일
RLE = diff([0 find(diff(A)~=0) length(A)]);
  댓글 수: 2
Ali Noori
Ali Noori 2015년 5월 27일
It works perfectly. Can you please find the average value of zeros and ones ex: average for zeros =4+5+3+2+2/4=4 average for ones= 5+3+6+2+2/4=4.5 thanks for your help again
Ali Noori
Ali Noori 2015년 5월 27일

sorry, average for zeros =4+5+3+2+2/5=3.2 average for ones= 5+3+6+2+2/5=3.6

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2015년 5월 27일
If you have the Image Processing Toolbox, this is how you do it:
% Create sample data
A = [0,0,0,0,1,1,1,1,1,0,0,0,0,0,1,1,1,0,0,0,1,1,1,1,1,1,0,0,1,1,0,0,1,1]
% Label the 0's.
labeled0 = bwlabel(~A)
% Count the number of 0's.
measurements0 = regionprops(labeled0, 'Area');
% Label the 1's.
labeled1 = bwlabel(A)
% Count the number of 1's.
measurements1 = regionprops(labeled1, 'Area');
% Stitch together into a single matrix.
zeroAreas = [measurements0.Area]
onesAreas = [measurements1.Area]
bothAreas = [zeroAreas;onesAreas]
% Reshape into a row vector.
finalOutput = bothAreas(:)'
  댓글 수: 1
Ali Noori
Ali Noori 2015년 5월 27일
Its works too, many thanks But the code for Mr. Roberson is much easier.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by