What code would I use to find the first 0 then count from there?
1 view (last 30 days)
Show older comments
Franchesca
on 21 Apr 2014
Answered: Walter Roberson
on 21 Apr 2014
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?
0 Comments
Accepted Answer
Image Analyst
on 21 Apr 2014
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().
0 Comments
More Answers (1)
Walter Roberson
on 21 Apr 2014
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?
0 Comments
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!