필터 지우기
필터 지우기

What code would I use to find the first 0 then count from there?

조회 수: 1 (최근 30일)
Franchesca
Franchesca 2014년 4월 21일
답변: Walter Roberson 2014년 4월 21일
I have a set of data which contains a block of zeros which I have wrote this code to count the zeros for:
for i=7:length(mydata)
no_of_zeros = size(mydata{i,1},1) - nnz(mydata{i,1}(:,5)); %number of zeros
no_of_frames = (no_of_zeros/4) % number of frames
time = ((no_of_frames/fps)/2) % time up
jumph(i,1)=((a*(time*time))/2) % jump height
end
However, some of the data has 2 blocks of zeros as they stepped off the force plate early so matlab includes these 0's in its calculation. So I need matlab to find the first zero in the column then count from there until the zeros stop and change to numbers and use that amount of zeros in the calculations.
How would I do this?

채택된 답변

Image Analyst
Image Analyst 2014년 4월 21일
Do you have the Image Processing Toolbox? You can find all the zeros stretches like this:
labeledSignal = bwlabel(mydata == 0);
% Measure lengths of all zero stretches.
measurements = regionprops(labeledSignal, 'Area', 'PixelIdxList');
% Extract from structure into a simple array of lengths.
lengthsOfAllZeroSegments = [measurements.Area];
% See where each segment of zeros starts and stops.
for k = 1 : length(lengthsOfAllZeroSegments )
startingElement(k) = measurements(k).PixelIdxList(1);
endingElement(k) = measurements(k).PixelIdxList(end);
end
If you want, you can throw out small stretches before measuring their lengths by using bwareaopen().

추가 답변 (1개)

Walter Roberson
Walter Roberson 2014년 4월 21일
Lvec = logical(mydata{i,1}(:,5));
z_nz_pos = strfind(Lvec, [1 0]);
nz_z_pos = strfind(Lvec, [0 1]);
no_of_zeros = nz_z_pos(1) - z_nz_pos(1);
Caution: this code will need to be adjusted if there is the possibility that the data starts with zeros, or ends with zeros that need to be considered (e.g., landed off of the plate), or is all zero or is all non-zero. The modification for starting or ending with 0 or all 0 is fairly easy, but what should be done for the no-zero case?

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by