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);
But then I have a message error : ??? Subscripted assignment dimension mismatch.
Thank you very much in advance for your help
No products are associated with this question.
OK, I can't seem to find a utility I had that does basically what you were/are looking for that I thought was around but here's a straightahead implementation using the above idea of diff()==2...
function pks = peaksbetween(x) % peaks from column vector x between zero crossings
ix=find(abs([0; diff(sign(x))])==2); % zero crossing loc's pks=zeros(length(ix)-1); % one less peak than x'ings for i=1:length(ix)-1 sx=x(ix(i):ix(i+1)-1); % subvector between x'ings [~,ipk]=max(abs(sx)); % peak location in subvector pks(i)=sx(ipk); % and signed max/min value end
To return the locations in the original vector means unwrapping the local location to the global--I left as "exercise for the student"... :)
If I can find the vectorized version I'll post it--I know it's around but this is a new install and it must not have gotten over from the utilities on the previous release or I put it somewhere other than I recall--altho which didn't find it so it's not on path... :(
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