前言
求解一个向量中,连续非零区域的数目。
% /************************************************************************ % * Copyright(c) 2017 ZRJ % * All rights reserved. % * % * File: none_zero_block_num.m % * Brief: 向量非零区域块数目 % * Version: 1.0 % * Author: ZRJ % * Email: happyamyhope@163.com % * Date: 2017/02/28 % * Reference: % * History: % * 20160919:向量非零区域块数目; % % ************************************************************************/ function block_num = none_zero_block_num( a ) % 向量非零区域块数目 % Nonzero regional block number %程序功能:Apply function to Get the Number of none zero block in one vector. %输入: % cdata -- input vector data. %输出: % block_num -- output of Nonzero regional block number. num = 0;%非零区域块数目 for i = 1 : 1 : size(a, 2) - 1 if a(i) > 0 && a(i+1) ==0 num = num + 1; end end if a(end) > 0 num = num + 1; end block_num = num; end % end function
完