## Profile values between a certain number in a matrix

Tags: matlab
By : J.Smith
Source: Stackoverflow.com
Question!

I am working on a hobby project where I have a matrix 1x1000 with numbers such as 0 30 50 70 100 50 60 0 50 40 30 20 50 0 and so on.

Now I would like to profile all the values in between the 0, so in my example above it would result in [30 50 70 100 50 60] and [50 40 30 20 50]. I have tried `range` but that is probably not the right way to go.

Furthermore, I would like to set a condition that it can only profile if there is a certain amount of numbers between the zeros, for instance 5.

Any help is greatly appreciated!

By : J.Smith

Use find to locate to non-zero numbers. Then find the start and end index of the different segments.

``````% Example input
A = [0 30 50 70 100 50 60 0 50 40 30 20 50 0];

% Find start and end point of segments
validIdx = find(A(:)~=0);
idxEdge = diff(validIdx) ``````
``` By : nilZ0r ```
``` Here is another option that does the follows: Does not assume the vector starts and ends with 0. Can handle series of 0 within the vector. Eliminates empty cells from the output. Preallocate the output, and loop only on the segments that are above the predefined size minsize = 5; % minimum size for segment V = (randi(10,50,1)-1)*10; % random vector generation zero_pos = find([0;V(:);0]==0); % all 0's position d = diff(zero_pos); % count how large is each segment % Ignore 0 and By : EBH I think that in this case a simple loop is the best solution: V = randi(10,500000,1)-1 %random vector generation V = [0;V;0]; %need to start and end with 0 pos = find(V==0); %find the 0's position for i = 1:length(pos)-1 M{i} = V(pos(i) 1:pos(i 1)-1); end ind = cellfun(@length,M); M(ind By : obchardon This video can help you solving your question :) By: admin ```
``` Related Questions Does anyone have experience creating a shared library in MATLAB? How can I stop MATLAB from returning until after a command-line script completes? What are some efficient ways to combine two structures in MATLAB? Can parallel traversals be done in MATLAB just as in Python? (adsbygoogle = window.adsbygoogle || []).push({}); Open source alternative to MATLAB's fmincon function? What alternatives are there to the MATLAB Editor? How can I generate a list of function dependencies in MATLAB? In MATLAB, can a class method act as a uicontrol callback without being public? How can I sort a 2-D array in MATLAB with respect to one column? MATLAB Environment Tweaks [closed] In MATLAB, how do I change the background color of a subplot? Reading input m-file in a main m-file Creating output m-file in matlab Running on 32 or 64 bit matlab? In Matlab, what does zpk([], [i, -i], 1) mean? MATLAB error: Undefined function or method X for input arguments of type 'double' What is the simplest way to export Excel data to Matlab? Best MATLAB toolbox that implements Support Vector Regression? [closed] Read and write from/to a binary file in Matlab Plot a data series beneath another one ```
``` ```
``` About Us    Contact Us    Legal    feedback    Copyright © 2015 - All Rights Reserved - www.4answered.com ```