Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
obtain independent vectors of non zeros

Subject: obtain independent vectors of non zeros

From: diana escalona

Date: 18 Dec, 2012 03:46:09

Message: 1 of 3

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)

Subject: obtain independent vectors of non zeros

From: Ryan

Date: 24 Dec, 2012 02:06:09

Message: 2 of 3

"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

Subject: obtain independent vectors of non zeros

From: Ryan

Date: 24 Dec, 2012 02:17:09

Message: 3 of 3

"Ryan" wrote in message <kb8dah$fk5$1@newscl01ah.mathworks.com>...
> "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


Or, maybe this:
regexp('16 17 32 0 0 63 79 80 0 0 0 113 0 129 130', '0', 'split')

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us