What code would I use to find the first 0 then count from there?
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
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().
0 件のコメント
その他の回答 (1 件)
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?
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Image Processing Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!