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:
find the maximum index of blocks of data

Subject: find the maximum index of blocks of data

From: Jose

Date: 10 Dec, 2008 16:52:02

Message: 1 of 12

Hi everyone, with the index inx, I detected the position of my data vector bigger
than a threshold thr.

thr= 4*median(abs(data)/0.6745)= 131
inx = find(data>thr)

         
inx= 238 data(238)= 222
         239 data(239)= 523
         240 data(240)=577
         241 data(241)=211
         454 data(454)= 308
         455 data(455)=526
         456 data(456) =361
         800
         ...
         ..
But, i am interested to store in a vector only the maximum index of every block of data,
i.e...block one (238, 239, 240,241)..I want to store the index 240, block 2 (454,455,456)...I want to store the index 455, and follow sucesively.

Anyone, can tell me (write the code) how I can do it to find the maximum index of my blocks of data?

Thanks in advance,

Jose.

Subject: find the maximum index of blocks of data

From: Donn Shull

Date: 10 Dec, 2008 18:06:02

Message: 2 of 12

"Jose " <jose.l.vega@gmail.com> wrote in message <ghos3i$ms1$1@fred.mathworks.com>...
> Hi everyone, with the index inx, I detected the position of my data vector bigger
> than a threshold thr.
>
> thr= 4*median(abs(data)/0.6745)= 131
> inx = find(data>thr)
>
>
> inx= 238 data(238)= 222
> 239 data(239)= 523
> 240 data(240)=577
> 241 data(241)=211
> 454 data(454)= 308
> 455 data(455)=526
> 456 data(456) =361
> 800
> ...
> ..
> But, i am interested to store in a vector only the maximum index of every block of data,
> i.e...block one (238, 239, 240,241)..I want to store the index 240, block 2 (454,455,456)...I want to store the index 455, and follow sucesively.
>
> Anyone, can tell me (write the code) how I can do it to find the maximum index of my blocks of data?
>
> Thanks in advance,
>
> Jose.

Hello Jose,

I think I understand what you are looking for. If I do then the steps I would use to solve this problem would be:

1. we need to know where there are jumps in the inx variable.

given:

inx = [238,239,240,241,454,455,456, 800, 801, 802, 803];

form

dif = diff(inx)

ans =

1 1 1 213 1 1 344 1 1 1

and

breaks = find(div >1)

breaks =

4, 7

2. We have found the big jumps in inx. Now lets mane it a vector we can iterate over

breaks = [0, breaks, numel(inx)]

breaks =

     0 4 7 11

3. Now use a simple loop

for index = 1:numel(breaks -1)
    maxDataThisBlock = max(data(breaks(i)+1:breaks(i+1));
    % do something with maxDataThisBlock here
end

hope this helps,

Donn

Subject: find the maximum index of blocks of data

From: Jose

Date: 10 Dec, 2008 19:16:02

Message: 3 of 12

"Donn Shull" <donn.shull.no_spam@aetoolbox.com> wrote in message <ghp0ea$3v6$1@fred.mathworks.com>...
> "Jose " <jose.l.vega@gmail.com> wrote in message <ghos3i$ms1$1@fred.mathworks.com>...
> > Hi everyone, with the index inx, I detected the position of my data vector bigger
> > than a threshold thr.
> >
> > thr= 4*median(abs(data)/0.6745)= 131
> > inx = find(data>thr)
> >
> >
> > inx= 238 data(238)= 222
> > 239 data(239)= 523
> > 240 data(240)=577
> > 241 data(241)=211
> > 454 data(454)= 308
> > 455 data(455)=526
> > 456 data(456) =361
> > 800
> > ...
> > ..
> > But, i am interested to store in a vector only the maximum index of every block of data,
> > i.e...block one (238, 239, 240,241)..I want to store the index 240, block 2 (454,455,456)...I want to store the index 455, and follow sucesively.
> >
> > Anyone, can tell me (write the code) how I can do it to find the maximum index of my blocks of data?
> >
> > Thanks in advance,
> >
> > Jose.
>
> Hello Jose,
>
> I think I understand what you are looking for. If I do then the steps I would use to solve this problem would be:
>
> 1. we need to know where there are jumps in the inx variable.
>
> given:
>
> inx = [238,239,240,241,454,455,456, 800, 801, 802, 803];
>
> form
>
> dif = diff(inx)
>
> ans =
>
> 1 1 1 213 1 1 344 1 1 1
>
> and
>
> breaks = find(div >1)
>
> breaks =
>
> 4, 7
>
> 2. We have found the big jumps in inx. Now lets mane it a vector we can iterate over
>
> breaks = [0, breaks, numel(inx)]
>
> breaks =
>
> 0 4 7 11
>
> 3. Now use a simple loop
>
> for index = 1:numel(breaks -1)
> maxDataThisBlock = max(data(breaks(i)+1:breaks(i+1));
> % do something with maxDataThisBlock here
> end
>
> hope this helps,
>
> Donn
Hello Donn, i am please with the step 1 and 2, but data(breaks(2)+1):breaks(3)=data(5):data(7), and it does not correspond with my data(454):data(454) that i am interested...
I think, the blocks are located, but we need some translation to get the right data
Any idea?

Subject: find the maximum index of blocks of data

From: Donn Shull

Date: 10 Dec, 2008 20:14:02

Message: 4 of 12

"Jose " <jose.l.vega@gmail.com> wrote in message <ghp4hi$fa4$1@fred.mathworks.com>...
> "Donn Shull" <donn.shull.no_spam@aetoolbox.com> wrote in message <ghp0ea$3v6$1@fred.mathworks.com>...
> > "Jose " <jose.l.vega@gmail.com> wrote in message <ghos3i$ms1$1@fred.mathworks.com>...
> > > Hi everyone, with the index inx, I detected the position of my data vector bigger
> > > than a threshold thr.
> > >
> > > thr= 4*median(abs(data)/0.6745)= 131
> > > inx = find(data>thr)
> > >
> > >
> > > inx= 238 data(238)= 222
> > > 239 data(239)= 523
> > > 240 data(240)=577
> > > 241 data(241)=211
> > > 454 data(454)= 308
> > > 455 data(455)=526
> > > 456 data(456) =361
> > > 800
> > > ...
> > > ..
> > > But, i am interested to store in a vector only the maximum index of every block of data,
> > > i.e...block one (238, 239, 240,241)..I want to store the index 240, block 2 (454,455,456)...I want to store the index 455, and follow sucesively.
> > >
> > > Anyone, can tell me (write the code) how I can do it to find the maximum index of my blocks of data?
> > >
> > > Thanks in advance,
> > >
> > > Jose.
> >
> > Hello Jose,
> >
> > I think I understand what you are looking for. If I do then the steps I would use to solve this problem would be:
> >
> > 1. we need to know where there are jumps in the inx variable.
> >
> > given:
> >
> > inx = [238,239,240,241,454,455,456, 800, 801, 802, 803];
> >
> > form
> >
> > dif = diff(inx)
> >
> > ans =
> >
> > 1 1 1 213 1 1 344 1 1 1
> >
> > and
> >
> > breaks = find(div >1)
> >
> > breaks =
> >
> > 4, 7
> >
> > 2. We have found the big jumps in inx. Now lets mane it a vector we can iterate over
> >
> > breaks = [0, breaks, numel(inx)]
> >
> > breaks =
> >
> > 0 4 7 11
> >
> > 3. Now use a simple loop
> >
> > for index = 1:numel(breaks -1)
> > maxDataThisBlock = max(data(breaks(i)+1:breaks(i+1));
> > % do something with maxDataThisBlock here
> > end
> >
> > hope this helps,
> >
> > Donn
> Hello Donn, i am please with the step 1 and 2, but data(breaks(2)+1):breaks(3)=data(5):data(7), and it does not correspond with my data(454):data(454) that i am interested...
> I think, the blocks are located, but we need some translation to get the right data
> Any idea?

Sorry about that Jose. Should have been:

maxDataThisBlock = max(data(inx([breaks(i)+1:breaks(i+1)]));

Donn

Subject: find the maximum index of blocks of data

From: Jose

Date: 11 Dec, 2008 10:45:04

Message: 5 of 12

"Jose " <jose.l.vega@gmail.com> wrote in message <ghos3i$ms1$1@fred.mathworks.com>...
> Hi everyone, with the index inx, I detected the position of my data vector bigger
> than a threshold thr.
>
> thr= 4*median(abs(data)/0.6745)= 131
> inx = find(data>thr)
>
>
> inx= 238 data(238)= 222
> 239 data(239)= 523
> 240 data(240)=577
> 241 data(241)=211
> 454 data(454)= 308
> 455 data(455)=526
> 456 data(456) =361
> 800
> ...
> ..
> But, i am interested to store in a vector only the maximum index of every block of data,
> i.e...block one (238, 239, 240,241)..I want to store the index 240, block 2 (454,455,456)...I want to store the index 455, and follow sucesively.
>
> Anyone, can tell me (write the code) how I can do it to find the maximum index of my blocks of data?
>
> Thanks in advance,
>
> Jose.

Many thanks Donn, I got it, but I have my last question to finish with it:

Any idea how you can preallocated xsn to avoid
growing the vector inside the loop?

This is my fnal code:

thr= 4*median(abs(data)/0.6745);
inx = find(data>thr)';
dif = diff(inx);
 
breaks = find(dif >1);
breaks=[0 breaks];
xc=zeros(1,numel(breaks)-1); % pre-allocated xc

xsn=[]; % Is possible pre-allocated xsn too?
for i = 1:numel(breaks)-1
xc(i) = max(data(inx(breaks(i)+1:breaks(i+1))));
inx2=find(data==xc(i),1,'first');
xp=data(inx2-20:inx2+20);
xsn=[xsn xp];
end

Thanks in advance,

Jose.

Subject: find the maximum index of blocks of data

From: John D'Errico

Date: 11 Dec, 2008 11:35:02

Message: 6 of 12

"Jose " <jose.l.vega@gmail.com> wrote in message <ghqqvg$mhb$1@fred.mathworks.com>...
> "Jose " <jose.l.vega@gmail.com> wrote in message <ghos3i$ms1$1@fred.mathworks.com>...
> > Hi everyone, with the index inx, I detected the position of my data vector bigger
> > than a threshold thr.
> >
> > thr= 4*median(abs(data)/0.6745)= 131
> > inx = find(data>thr)
> >
> >
> > inx= 238 data(238)= 222
> > 239 data(239)= 523
> > 240 data(240)=577
> > 241 data(241)=211
> > 454 data(454)= 308
> > 455 data(455)=526
> > 456 data(456) =361
> > 800
> > ...
> > ..
> > But, i am interested to store in a vector only the maximum index of every block of data,
> > i.e...block one (238, 239, 240,241)..I want to store the index 240, block 2 (454,455,456)...I want to store the index 455, and follow sucesively.
> >
> > Anyone, can tell me (write the code) how I can do it to find the maximum index of my blocks of data?
> >
> > Thanks in advance,
> >
> > Jose.
>
> Many thanks Donn, I got it, but I have my last question to finish with it:
>
> Any idea how you can preallocated xsn to avoid
> growing the vector inside the loop?
>
> This is my fnal code:
>
> thr= 4*median(abs(data)/0.6745);
> inx = find(data>thr)';
> dif = diff(inx);
>
> breaks = find(dif >1);
> breaks=[0 breaks];
> xc=zeros(1,numel(breaks)-1); % pre-allocated xc
>
> xsn=[]; % Is possible pre-allocated xsn too?
> for i = 1:numel(breaks)-1
> xc(i) = max(data(inx(breaks(i)+1:breaks(i+1))));
> inx2=find(data==xc(i),1,'first');
> xp=data(inx2-20:inx2+20);
> xsn=[xsn xp];
> end
>
> Thanks in advance,
>
> Jose.

I know I'm coming in late here, but I'd do all of
this quite differently. A bit simpler though, and
only about 3 lines or so for the entire mess.

Assume that I've got two vectors, inx and data.

I know that inx actually is used to index into
data, but I'll be lazy here.

inx = [238 239 240 241 454 455 456 800];
data = [222 523 577 211 308 526 361 937];

breaks = find(diff(inx)~=1);
indicators = zeros(size(inx));
indicators(breaks+1) = 1;
indicators = 1+cumsum(indicators);
maxdata = accumarray(indicators(:),data(:),[],@max)

maxdata =
   577
   526
   937

This should be both simpler and considerably faster
than your code.

John

Subject: find the maximum index of blocks of data

From: Jose

Date: 11 Dec, 2008 12:02:01

Message: 7 of 12

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <ghqtt6$sq5$1@fred.mathworks.com>...
> "Jose " <jose.l.vega@gmail.com> wrote in message <ghqqvg$mhb$1@fred.mathworks.com>...
> > "Jose " <jose.l.vega@gmail.com> wrote in message <ghos3i$ms1$1@fred.mathworks.com>...
> > > Hi everyone, with the index inx, I detected the position of my data vector bigger
> > > than a threshold thr.
> > >
> > > thr= 4*median(abs(data)/0.6745)= 131
> > > inx = find(data>thr)
> > >
> > >
> > > inx= 238 data(238)= 222
> > > 239 data(239)= 523
> > > 240 data(240)=577
> > > 241 data(241)=211
> > > 454 data(454)= 308
> > > 455 data(455)=526
> > > 456 data(456) =361
> > > 800
> > > ...
> > > ..
> > > But, i am interested to store in a vector only the maximum index of every block of data,
> > > i.e...block one (238, 239, 240,241)..I want to store the index 240, block 2 (454,455,456)...I want to store the index 455, and follow sucesively.
> > >
> > > Anyone, can tell me (write the code) how I can do it to find the maximum index of my blocks of data?
> > >
> > > Thanks in advance,
> > >
> > > Jose.
> >
> > Many thanks Donn, I got it, but I have my last question to finish with it:
> >
> > Any idea how you can preallocated xsn to avoid
> > growing the vector inside the loop?
> >
> > This is my fnal code:
> >
> > thr= 4*median(abs(data)/0.6745);
> > inx = find(data>thr)';
> > dif = diff(inx);
> >
> > breaks = find(dif >1);
> > breaks=[0 breaks];
> > xc=zeros(1,numel(breaks)-1); % pre-allocated xc
> >
> > xsn=[]; % Is possible pre-allocated xsn too?
> > for i = 1:numel(breaks)-1
> > xc(i) = max(data(inx(breaks(i)+1:breaks(i+1))));
> > inx2=find(data==xc(i),1,'first');
> > xp=data(inx2-20:inx2+20);
> > xsn=[xsn xp];
> > end
> >
> > Thanks in advance,
> >
> > Jose.
>
> I know I'm coming in late here, but I'd do all of
> this quite differently. A bit simpler though, and
> only about 3 lines or so for the entire mess.
>
> Assume that I've got two vectors, inx and data.
>
> I know that inx actually is used to index into
> data, but I'll be lazy here.
>
> inx = [238 239 240 241 454 455 456 800];
> data = [222 523 577 211 308 526 361 937];
>
> breaks = find(diff(inx)~=1);
> indicators = zeros(size(inx));
> indicators(breaks+1) = 1;
> indicators = 1+cumsum(indicators);
> maxdata = accumarray(indicators(:),data(:),[],@max)
>
> maxdata =
> 577
> 526
> 937
>
> This should be both simpler and considerably faster
> than your code.
>
> John


Thanks very much John!! but my "inx" vector always is smaller than my "data" vector:
inx = [238 239 240 241 454]
data = [222 523 577 211 308 526 361 937]

and I afraid for it because I found this mistake:

??? Error using ==> accumarray
Second input VAL must be a vector with one element for each row in SUBS, or a scalar.

Could you help me with it, when the vectors have different sizes?

Subject: find the maximum index of blocks of data

From: John D'Errico

Date: 11 Dec, 2008 12:54:01

Message: 8 of 12

"Jose " <jose.l.vega@gmail.com> wrote in message <ghqvfp$iq1$1@fred.mathworks.com>...
> "John D'Errico" <woodchips@rochester.rr.com> wrote in message <ghqtt6$sq5$1@fred.mathworks.com>...

> > I know I'm coming in late here, but I'd do all of
> > this quite differently. A bit simpler though, and
> > only about 3 lines or so for the entire mess.
> >
> > Assume that I've got two vectors, inx and data.
> >
> > I know that inx actually is used to index into
> > data, but I'll be lazy here.
> >
> > inx = [238 239 240 241 454 455 456 800];
> > data = [222 523 577 211 308 526 361 937];
> >
> > breaks = find(diff(inx)~=1);
> > indicators = zeros(size(inx));
> > indicators(breaks+1) = 1;
> > indicators = 1+cumsum(indicators);
> > maxdata = accumarray(indicators(:),data(:),[],@max)
> >
> > maxdata =
> > 577
> > 526
> > 937
> >
> > This should be both simpler and considerably faster
> > than your code.
> >
> > John
>
>
> Thanks very much John!! but my "inx" vector always is smaller than my "data" vector:
> inx = [238 239 240 241 454]
> data = [222 523 577 211 308 526 361 937]
>
> and I afraid for it because I found this mistake:
>
> ??? Error using ==> accumarray
> Second input VAL must be a vector with one element for each row in SUBS, or a scalar.
>
> Could you help me with it, when the vectors have different sizes?

The only difference is in the last line.

maxdata = accumarray(indicators(:),data(inx(:)),[],@max);

The problem is, I misread your post. This code
gives you the value of the maximum, not the
index that it occurs at. To fix that is not too hard.
I need a function that just returns the index of
the maximum. This will do:


function ind = maxind(vec)
[junk,ind] = max(vec);


I could probably have done it with an anonymous
function too, had I been willing to think for a
minute. Save the function as an m-file. Now call
accumarray this way...

maxindex = accumarray(indicators(:),data(inx(:)),[],@maxind);
maxindex(2:end) = maxindex(2:end) + breaks(:);
maxindex = inx(maxindex);

John

Subject: find the maximum index of blocks of data

From: Jose

Date: 11 Dec, 2008 13:07:02

Message: 9 of 12

Hi John, I understood it, and you are rigth because i am interested in the index
when the maximum occurrs...but my question is:
this last line
maxdata = accumarray(indicators(:),data(inx(:)),[],@max);
does not work, when i have different sizes in the vextors "inx" and "data".
some idea?
Cheers,
Jose.


"John D'Errico" <woodchips@rochester.rr.com> wrote in message <ghr2h9$rn5$1@fred.mathworks.com>...
> "Jose " <jose.l.vega@gmail.com> wrote in message <ghqvfp$iq1$1@fred.mathworks.com>...
> > "John D'Errico" <woodchips@rochester.rr.com> wrote in message <ghqtt6$sq5$1@fred.mathworks.com>...
>
> > > I know I'm coming in late here, but I'd do all of
> > > this quite differently. A bit simpler though, and
> > > only about 3 lines or so for the entire mess.
> > >
> > > Assume that I've got two vectors, inx and data.
> > >
> > > I know that inx actually is used to index into
> > > data, but I'll be lazy here.
> > >
> > > inx = [238 239 240 241 454 455 456 800];
> > > data = [222 523 577 211 308 526 361 937];
> > >
> > > breaks = find(diff(inx)~=1);
> > > indicators = zeros(size(inx));
> > > indicators(breaks+1) = 1;
> > > indicators = 1+cumsum(indicators);
> > > maxdata = accumarray(indicators(:),data(:),[],@max)
> > >
> > > maxdata =
> > > 577
> > > 526
> > > 937
> > >
> > > This should be both simpler and considerably faster
> > > than your code.
> > >
> > > John
> >
> >
> > Thanks very much John!! but my "inx" vector always is smaller than my "data" vector:
> > inx = [238 239 240 241 454]
> > data = [222 523 577 211 308 526 361 937]
> >
> > and I afraid for it because I found this mistake:
> >
> > ??? Error using ==> accumarray
> > Second input VAL must be a vector with one element for each row in SUBS, or a scalar.
> >
> > Could you help me with it, when the vectors have different sizes?
>
> The only difference is in the last line.
>
> maxdata = accumarray(indicators(:),data(inx(:)),[],@max);
>
> The problem is, I misread your post. This code
> gives you the value of the maximum, not the
> index that it occurs at. To fix that is not too hard.
> I need a function that just returns the index of
> the maximum. This will do:
>
>
> function ind = maxind(vec)
> [junk,ind] = max(vec);
>
>
> I could probably have done it with an anonymous
> function too, had I been willing to think for a
> minute. Save the function as an m-file. Now call
> accumarray this way...
>
> maxindex = accumarray(indicators(:),data(inx(:)),[],@maxind);
> maxindex(2:end) = maxindex(2:end) + breaks(:);
> maxindex = inx(maxindex);
>
> John

Subject: find the maximum index of blocks of data

From: John D'Errico

Date: 11 Dec, 2008 13:29:03

Message: 10 of 12

"Jose " <jose.l.vega@gmail.com> wrote in message <ghr39m$849$1@fred.mathworks.com>...
> Hi John, I understood it, and you are rigth because i am interested in the index
> when the maximum occurrs...but my question is:
> this last line
> maxdata = accumarray(indicators(:),data(inx(:)),[],@max);
> does not work, when i have different sizes in the vextors "inx" and "data".
> some idea?
> Cheers,
> Jose.

If your data vector is created as I first did, where data
and inx were the same shape, then this will work:

maxdata = accumarray(indicators(:),data,[],@max);

If your data vector is a column vector, and defined
such that inx is an index into that vector, this this is
correct:

maxdata = accumarray(indicators(:),data(inx(:)),[],@max);

Is your data a row vector? If so, then accumarray
requires a column vector as the input here. So this
will work:

maxdata = accumarray(indicators(:), ...
     reshape(data(inx),[],1),[],@max);

To get the index of the maximum, use accumarray
as above, but with the maxind function I wrote for
you.

maxindex = accumarray(indicators(:), ...
     reshape(data(inx),[],1),[],@maxind);

John

Subject: find the maximum index of blocks of data

From: Jose

Date: 11 Dec, 2008 14:18:03

Message: 11 of 12





"John D'Errico" <woodchips@rochester.rr.com> wrote in message <ghr4iv$pjj$1@fred.mathworks.com>...
> "Jose " <jose.l.vega@gmail.com> wrote in message <ghr39m$849$1@fred.mathworks.com>...
> > Hi John, I understood it, and you are rigth because i am interested in the index
> > when the maximum occurrs...but my question is:
> > this last line
> > maxdata = accumarray(indicators(:),data(inx(:)),[],@max);
> > does not work, when i have different sizes in the vextors "inx" and "data".
> > some idea?
> > Cheers,
> > Jose.
>
> If your data vector is created as I first did, where data
> and inx were the same shape, then this will work:
>
> maxdata = accumarray(indicators(:),data,[],@max);
>
> If your data vector is a column vector, and defined
> such that inx is an index into that vector, this this is
> correct:
>
> maxdata = accumarray(indicators(:),data(inx(:)),[],@max);
>
> Is your data a row vector? If so, then accumarray
> requires a column vector as the input here. So this
> will work:
>
> maxdata = accumarray(indicators(:), ...
> reshape(data(inx),[],1),[],@max);
>
> To get the index of the maximum, use accumarray
> as above, but with the maxind function I wrote for
> you.
>
> maxindex = accumarray(indicators(:), ...
> reshape(data(inx),[],1),[],@maxind);
>
> John


Thank you very much John, it works very well and I improve my speed in 0.4 seconds respect my last code.
My last question to try to optimize it definitelly:
Is possible pre-allocated xsn to avoid xsn growns inside of the loop?
I think, if I do it, I can save time.

My code:

ns=20;
xsn=[];
for i = 1:nt
 xp=data(maxindex(i)-ns:maxindex(i)+ns);
 xsn=[xsn xp];
end

Thanks in advance,
Jose.

Subject: find the maximum index of blocks of data

From: John D'Errico

Date: 11 Dec, 2008 16:00:20

Message: 12 of 12

"Jose " <jose.l.vega@gmail.com> wrote in message <ghr7er$7qk$1@fred.mathworks.com>...
>
>
>
>
> "John D'Errico" <woodchips@rochester.rr.com> wrote in message <ghr4iv$pjj$1@fred.mathworks.com>...
> > "Jose " <jose.l.vega@gmail.com> wrote in message <ghr39m$849$1@fred.mathworks.com>...
> > > Hi John, I understood it, and you are rigth because i am interested in the index
> > > when the maximum occurrs...but my question is:
> > > this last line
> > > maxdata = accumarray(indicators(:),data(inx(:)),[],@max);
> > > does not work, when i have different sizes in the vextors "inx" and "data".
> > > some idea?
> > > Cheers,
> > > Jose.
> >
> > If your data vector is created as I first did, where data
> > and inx were the same shape, then this will work:
> >
> > maxdata = accumarray(indicators(:),data,[],@max);
> >
> > If your data vector is a column vector, and defined
> > such that inx is an index into that vector, this this is
> > correct:
> >
> > maxdata = accumarray(indicators(:),data(inx(:)),[],@max);
> >
> > Is your data a row vector? If so, then accumarray
> > requires a column vector as the input here. So this
> > will work:
> >
> > maxdata = accumarray(indicators(:), ...
> > reshape(data(inx),[],1),[],@max);
> >
> > To get the index of the maximum, use accumarray
> > as above, but with the maxind function I wrote for
> > you.
> >
> > maxindex = accumarray(indicators(:), ...
> > reshape(data(inx),[],1),[],@maxind);
> >
> > John
>
>
> Thank you very much John, it works very well and I improve my speed in 0.4 seconds respect my last code.
> My last question to try to optimize it definitelly:
> Is possible pre-allocated xsn to avoid xsn growns inside of the loop?
> I think, if I do it, I can save time.
>
> My code:
>
> ns=20;
> xsn=[];
> for i = 1:nt
> xp=data(maxindex(i)-ns:maxindex(i)+ns);
> xsn=[xsn xp];
> end
>
> Thanks in advance,
> Jose.

Why not preallocate xsn to be the correct size
before the loop? You know EXACTLY what size it
will be when done. It looks like the size of the
array will be [20,nt]. Then use a matrix index
to stuff the numbers into xsn, as opposed to
concatenation to build it.

John

Tags for 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