Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: obtain independent vectors of non zeros
Date: Mon, 24 Dec 2012 02:06:09 +0000 (UTC)
Organization: HSBC
Lines: 53
Message-ID: <kb8dah$fk5$1@newscl01ah.mathworks.com>
References: <kaoou1$3uv$1@newscl01ah.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: www-04-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1356314769 16005 172.30.248.35 (24 Dec 2012 02:06:09 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 24 Dec 2012 02:06:09 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 2921053
Xref: news.mathworks.com comp.soft-sys.matlab:785314

"diana escalona" wrote in message <kaoou1$3uv$1@newscl01ah.mathworks.com>...
> How can I obtain cell array whenever I find a zero or zeros, for example: 
> 
> a=[16 17 32 0 0 63 79 80 0 0 0 113 0 129 130]
> 
> I want to obtain subsets of non zeros and their sum
> 
> a1={16 17 32 }  and their sum1= sum(a1)
> 
> a2={63 79 80}  and their sum2= sum(a2)
> 
> a3={113}  and their sum3= sum(a3)
> 
> 
> a4={129 130 }  and their sum4= sum(a4)



Something like this?
string    = '16 17 32 0 0 63 79 80 0 0 0 113 0 129 130';
delimiter = '0';    % For tabs, use: delimiter = sprintf('\t');

% Find the delimiters
delimIdx = find(string == delimiter);

% Pretend there are delimiters at the beginning and end, for the loop below
delimIdx = [0  delimIdx  length(string)+1];

% Preallocate cell array to hold substrings
subStrings = cell(1, length(delimIdx) - 1);

% Process each element
for i = 1:length(subStrings)

    % Find the text between the delimiters
    %(don't include the delimiters)
    startOffset = delimIdx(i)   + 1;
    endOffset   = delimIdx(i+1) - 1;

    % Get the element
    txt = string(startOffset:endOffset);

    % Attempt conversion to number
    num = sscanf(txt, '%f');

    % Number conversion successful if no error message
    if isempty(num)
        subStrings{i} = txt;
    else
        subStrings{i} = num;
    end

end