How would I extract the first and last nonzero numbers from this array?

4 views (last 30 days)
So I am trying to grab the first and last numbers in the array multiple times. For example if I wanted to do it on this array.
[0 0 0 0 0 0 0 1 2 2 2 3 0 0 0 0 0 3 2 2 2 2 7 0 0 0 0 0 0 0]
Lets say I wanted the 1 and 3 in the first set of numbers and the 3 and 7 in the next set of numbers. How would I do this?

Answers (3)

Stephen23
Stephen23 on 14 Sep 2017
Edited: Stephen23 on 14 Sep 2017
Simpler:
>> V = [0,0,0,0,0,0,0,1,2,2,2,3,0,0,0,0,0,3,2,2,2,2,7,0,0,0,0,0,0,0];
>> D = diff([0,V,0]==0);
>> [V(D<0);V(D(2:end)>0)] % each column is one pair of values
ans =
1 3
3 7
>>
  3 Comments
Stephen23
Stephen23 on 14 Sep 2017
@Brandon Fox: my pleasure, I hope it helps. You can accept the answer that helped you most resolve your original question: this is the easiest way for you to show your appreciation to the volunteers who helped you.
Brandon Steinlein
Brandon Steinlein on 6 Nov 2020
Is it possible to pull the associated position (column/row) in the old matrix of the numbers in the new matrix?

Sign in to comment.


Robert U
Robert U on 14 Sep 2017
Hi Brandon Fox:
There are several cases that should be covered in case you want to create a robust function:
  1. vector starts with a number not zero
  2. vector ends with a number not zero
  3. single numbers in line not zero
A starting approach is to capture changes in the vector might be
function [ numbers ] = extract_nonzero( varargin )
if nargin < 1
input = [0 0 0 0 0 0 0 1 2 2 2 3 0 0 0 0 0 3 2 2 2 2 7 0 0 0 0 0 0 0];
elseif nargin > 1
error('Number of inputs is not supported.')
else
input = varargin{1};
end
% Append zeros to capture first and last number
input = [0 input 0];
% create a shifted vector
input_1 = [input(2:end) 0];
% find changes in input vector and create output
numbers(:,1) = input_1( diff( abs( input~=0 ) )>0 );
numbers(:,2) = input( diff( abs( input~=0 ) )<0 );
end
Here, single values are counted twice: first as start of elements of interest and as end.
Kind regards,
Robert
  2 Comments
Stephen23
Stephen23 on 14 Sep 2017
Edited: Stephen23 on 14 Sep 2017
Note that:
  • varargin is not required: a simple variable should be used. Code hinting and code-completion are not useful with varargin. Compare varargin:
vs. a named input argument:
  • Why bother to check the number of inputs (a bit pointless, see above), but not check what the inputs are: Type, size, range, ...?
  • Shadowing the inbuilt input is not a good idea.
  • Logical vectors only contain equivalent of zero and one, so abs is totally superfluous.
  • Unnecessary duplication of data: input and input1 are almost the same. This is not good data design.
See my answer for much simpler code.

Sign in to comment.


Andrei Bobrov
Andrei Bobrov on 14 Sep 2017
Edited: Andrei Bobrov on 14 Sep 2017
a = [0 0 0 0 0 0 0 1 2 2 2 3 0 0 0 0 0 10 2 2 2 2 7 0 0 0 0 0 0 0];
b = a > 0;
out = [a(strfind(b,[0 1])+1);a(strfind(b,[1 0]))];

Categories

Find more on Creating and Concatenating Matrices 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!