How can I find the maximum absolute value between two zeros ?
Show older comments
Hello everyone
My goal is to find the maximums/minimums between two zeros and when it occurs for some velocity data that are in the form of a column vector.
I tried to find where are zeros upward, find where are zeros downward, and then to find the maximum/minimum in between.
The problem is that my code does not work and I do not understand why. I'm pretty sure I must not be far for the good answer.
How can I find the maximum of U between an upward going and a downward going zero and the minimum of U between a downward going and an upward going zero ?
My code is:
indx=find(U(1:end-1,1).*U(2:end,1) < 0);
new_U=zeros(length(indx),1);
for k=1:length(indx)
new_U(k,1)=max(U(indx(k))):U(indx(k+1));
end
But then I have a message error : ??? Subscripted assignment dimension mismatch.
Thank you very much in advance for your help
2 Comments
Azzi Abdelmalek
on 23 Jul 2013
Give a short example
dpb
on 23 Jul 2013
for k=1:length(indx)
new_U(k,1)=max(U(indx(k))):U(indx(k+1));
end
error : ??? Subscripted assignment dimension mismatch.
Yes, there's no guarantee the number of elements from one set of indices is the same length as the next. If you want to save these subvectors then you'll have to use a cell array to handle the varying lengths.
Another way to find zero crossings is to look at
d=[0 diff(sign(U))];
find(d==2) % +ive crossings
find(d==-2) % -ive crossings
There are also a number of peak-finding routines on the FEX or findpeaks() in the Signal Processing Toolbox (altho iirc it doesn't isolate on the zero crossing intervals)
Accepted Answer
More Answers (1)
dpb
on 27 Jul 2013
Updated version that also returns locations in original vector. I did for simplicity restrict the input to a column vector; just add logic to ensure correct orientation of the pieces internally if want to handle either...or just brute-force a row vector to a column vector internally and then return the same orientation as the input--that's actually probably the better idea but will leave as "exercise for the student" to choose/implement if wish.
function [pks, ipks] = peaksbetween(x)
% peaks and locations from vector x between zero crossings and location
if size(x,1)==1, error('X must be a column vector'), end
if numel(x)~=length(x),error('X must be a column vector'), end
ix=find(abs(diff(sign(x)))==2)+1; % zero crossing loc's
L=length(ix)-1; % one less peak than x'ings
ipk=zeros(L,1);
for i=1:length(ix)-1
sx=x(ix(i):ix(i+1)-1); % subvector between x'ings
[~,ipk(i)]=max(abs(sx)); % peak location in subvector
end
ipks=ix(1:end-1)+ipk-1; % max loc in original vector
pks=x(ipks); % and signed max/min value
end
1 Comment
dpb
on 27 Jul 2013
One more variant...I'm still learning about accumarray and friends... :)
Using a local function to return the indices from max() then accumarray to remove the explicit loop. Too bad there's no syntax for returning alternate return values only.
function [pks, ipks] = peaksbetween(x)
% peaks and locations from vector x between zero crossings and location
if size(x,1)==1, error('X must be a column vector'), end
if numel(x)~=length(x),error('X must be a column vector'), end
ix=find(abs(diff(sign(x)))==2)+1; % zero crossing loc's
L=length(ix)-1; % one less peak than x'ings
ipk=accumarray([1:L]',[1:L]',[], @(i) imax(abs(x(ix(i):ix(i+1)-1))) );
ipks=ix(1:L)+ipk-1; % max loc in original vector
pks=x(ipks); % and signed max/min value
end
function idx=imax(x)
% Return max() indices as first argument
[~,idx]=max(x);
end
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!