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

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by Christopher
on 10 Oct 2013

Hello everyone,

I have a data set that I am trying to break up based upon peaks in the data. Essentially I finding the peaks and the corresponding locations. From there I am seperationg the data from loation 1 to 2, 2 to 3, etcc. I will illustrate this with my code below.

load=data(:,3) % just a column vector of data

[Maxload,location]=findpeaks(load,'MINPEAKDISTANCE',25000); % Finding peaks and location

Then what I am trying to do is build indivdual vectors from peak 1 to peak 2, peak 2 to peak 3, etc. I can do it by doing the following:

peak1=load(location(1):location(2)) peak2=load(location(2):location(3)) etc....

However, there are many peaks, so I need a way to automate this procedure. I have tried several times, but cannot get it to work. I know this should be rather simple I would think. Any help would be great.

Answer by Jos (10584)
on 10 Oct 2013

First of all, you do not want to have a variable named "load" as it clashes with the important function load Second, things become easy when you store the individual vectors in a cell array. Then a simple for-loop suffices:

MyLoad=data(:,3) % just a column vector of data [Maxload,location]=findpeaks(MyLoad,'MINPEAKDISTANCE',25000); % Finding peaks and location

location = [1 ; location(:) ; numel(MyLoad)] ; Nloc = numel(location) ; MyPeak = cell(Nloc-1,1) ; for k = 1:Nloc-1 MyPeak{k} = MyLoad(location(k):location(k+1)) ; end

Easy to debug, easy to maintain, and easy to explain ...

Show 3 older comments

Christopher
on 10 Oct 2013

Jos, I got it working, but how do I display the results? This is just simply returning a vector with the location points?

Cedric Wannaz
on 10 Oct 2013

`MyPeak` is a cell array of chunks of `MyLoad`. Note that Jos added the first and the last element positions to the list of locations (not sure of that's what you wanted, but you can easily remove them if not). Assume that your data, `MyLoad` is made of 100 elements, with two peaks at locations 27 and 64. Then cell 1 of MyPeak

MyPeak{1}

contains `MyLoad(1:27)`, cell 2

MyPeak{2}

contains `MyLoad(27:64)`, and cell 3

MyPeak{3}

contains `MyLoad(64:100)`. The important point about this solution is that the chuncks of data which are numeric arrays, are store in a cell array. Addressing cell arrays is done using curly brackets when you want to access cells content. Note that you can combine subs for indexing the cell array and subs for indexing the content, so, if you wanted to access for example element 10 of the first chunk, you could do it with

x = MyPeak{1} ; y = x(10) ;

but also as follows

y = MyPeak{1}(10) ;

## 2 Comments

## dpb (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/89792#comment_173424

gotta' run, but one way -- use

accumarrayw/ the inputs fromfindpeaksto build a cell array of the resulting array elements specified per your above assignment. NB: have to set the'uniformoutput'property to False to handle the different lengths.## Cedric Wannaz (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/89792#comment_173426

So peaks are present in all series, i.e.

load(location(1))is present inpeak1andpeak2?Side note: avoid using

loadas a variable name, because it conflicts with function LOAD.