How would I extract the first and last nonzero numbers from this array?
Afficher commentaires plus anciens
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?
Réponses (3)
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 commentaires
Brandon Fox
le 14 Sep 2017
Stephen23
le 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
le 6 Nov 2020
Is it possible to pull the associated position (column/row) in the old matrix of the numbers in the new matrix?
Robert U
le 14 Sep 2017
Hi Brandon Fox:
There are several cases that should be covered in case you want to create a robust function:
- vector starts with a number not zero
- vector ends with a number not zero
- 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 commentaires
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.
Robert U
le 14 Sep 2017
True, that was sloppy.
Andrei Bobrov
le 14 Sep 2017
Modifié(e) : Andrei Bobrov
le 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]))];
1 commentaire
Andrei Bobrov
le 14 Sep 2017
fixed
Catégories
En savoir plus sur Resizing and Reshaping Matrices dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!