Median(here it's middle ) of vector/array of every two adjacent values in array (value(i+1)-value(i)).

Hi guys!
I've signal called y1 it's sinusoidal and its amplitude is y axis, x's axis is the time in sec.
I have a have a vector(matrics) called t which its size 1x213 , for instance t=[1 2 3 20 100 50 30 32 44 50 90 10 3 4 6 8 9 30 20 2 1 3 ...etc] (integer values)
I want to take every the difference/subtraction between every two values of t and find the middle of that difference , how could I implement that in matlab?
for more clarifications I want to do difference between each two adjacent point in my array t, in other words to do this diff(i)=t(i+1)-t(i) and find middle of each diff(i) (actually it's the middle of the each differences middle(i)=diff(i)/2 );
if we imagine that every the differences are like "lines" , I want to find the middle of each lines(of each subtraction/differences)
could anyone help me how do I implement that in matlab? thanks alot for your assistance!

Answers (1)

Not so sure why this would be puzzling...
tm=diff(t)/2;
You have one few differences than elements of t, of course.

8 Comments

but my problem is how do I find difference between two values of my array while looping in matlab.
is diff(t) gives me the difference between each two adjacent values(t(i+1)-t(i)) ?
all what I I want to do is a code the calculate the difference between two adjacent values (t(i+1)-t(i)) in my array t and then I take the middle of each difference. the difference values for each two adjacent values put them in another array ..
could you help me how do I implement that in matlab? thanks alot
That's exactly what you'll get from the above -- you don't need any loops (and should strive to avoid them in MATLAB; that's why it's called "MATrix LABoratory")
Look up
doc diff
and try the above.
Thanks alot! appreciated now I understand!
but maybe help me in this? it's in the same context question.
Im trying to simple it my explanation much!
I have two array, one array called array y=[1 -2 3 -2] -integer values,
the other array called replication=[1 2 3 4 ];
I check the for evey value of y at index i, y(i) >0 ? if so then I add to my output array 1 but how many times? here now I check on the same index i the array called replication and the value of replication(i) is actually the times I should I add zero or ones to my output array. (the decision if I add value zero or one at index i to my output array is related to array y, if y(i) >0 I add 1 otherwise I add zero. and how many times I add zero or ones it's related to the value at array replication at index i)
in my current example the output should be this: [1 0 0 1 1 1 0 0 0 0 ] .
once again I added just number 1 one times because y(i=1)>0 and the replication at index i = replication(i) is 1 (one time) so I add the number 1 just one times.
now at i=2, I added two zeros because at y(i=2)<0 , so I add zero to my output array, how many times? I check replication(i=2) =2 (two times) then I add zero two times to my output array ..
etc
how could I do that in matlab? thanks alot!
You'll be amazed! :)
y=[1 -2 3 -2]; repl=[1:4];
>> repelem(y>0,repl)
ans =
1×10 logical array
1 0 0 1 1 1 0 0 0 0
>>
There's a marvelous illustration of the power of MATLAB with its rich vocabulary (a veritable cornucopia as it were!!!) .
Granted, finding the above is probably something most users won't think of until they've had a fairly significant amount of time to begin to become familiar with the vocabulary.
It's unfortunate TMW (The Mathworks, makers of MATLAB) have neutered the old HELP command. Before, it would list the various areas that are the sections in the doc and the one-line help line for each function in the category. That was a marvelous way to be able to look at what functions are available in the general area and get an idea of those that might be useful for a task at hand when not sure what might be the best tool.
AFAICT there's nowhere now there is such a succinct listing; a major loss. The continual addition of new toolboxes and class libraries and all made the top level listing so long I guess rather than try to fix it they just threw in the towel figuring everybody wants pretty now, and only us old fossils like me still use and find command line tools more helpful as often as not.
As follow up to the sidebar complaint made above about help...
If you don't know what category repelem belongs to, you can find out by
> which repelem
built-in (C:\ML_R2019b\toolbox\matlab\elmat\repelem)
>>
which shows it's part of the "elementary matrices" subset of the base MATLAB product...one of the subdirectories on the MATLABPATH.
Knowing that, one can find out what are all the functions in that category (presuming TMW has kept up the documentation in the top level help file and not quit updating the help to concentrate all on the documentation path--I don't know about that, I haven't thought to try to see).
That would give one--
>> help elmat
Elementary matrices and matrix manipulation.
Elementary matrices.
zeros - Zeros array.
ones - Ones array.
eye - Identity matrix.
repmat - Replicate and tile array.
repelem - Replicate elements of an array.
linspace - Linearly spaced vector.
logspace - Logarithmically spaced vector.
freqspace - Frequency spacing for frequency response.
meshgrid - X and Y arrays for 3-D plots.
accumarray - Construct an array with accumulation.
: - Regularly spaced vector and index into matrix.
Basic array information.
size - Size of array.
length - Length of vector.
ndims - Number of dimensions.
numel - Number of elements.
disp - Display matrix or text.
isempty - True for empty array.
isequal - True if arrays are numerically equal.
isequaln - True if arrays are numerically equal, treating NaNs as equal.
Matrix manipulation.
cat - Concatenate arrays.
reshape - Reshape array.
diag - Diagonal matrices and diagonals of matrix.
blkdiag - Block diagonal concatenation.
tril - Extract lower triangular part.
triu - Extract upper triangular part.
fliplr - Flip matrix in left/right direction.
flipud - Flip matrix in up/down direction.
flip - Flip the order of elements.
rot90 - Rotate matrix 90 degrees.
: - Regularly spaced vector and index into matrix.
find - Find indices of nonzero elements.
end - Last index.
sub2ind - Linear index from multiple subscripts.
ind2sub - Multiple subscripts from linear index.
bsxfun - Binary singleton expansion function.
Multi-dimensional array functions.
ndgrid - Generate arrays for N-D functions and interpolation.
permute - Permute array dimensions.
ipermute - Inverse permute array dimensions.
shiftdim - Shift dimensions.
circshift - Shift array circularly.
squeeze - Remove singleton dimensions.
Array utility functions.
isscalar - True for scalar.
isvector - True for vector.
isrow - True for row vector.
iscolumn - True for column vector.
ismatrix - True for matrix.
Special variables and constants.
eps - Floating point relative accuracy.
realmax - Largest positive floating point number.
realmin - Smallest positive floating point number.
intmax - Largest positive integer value.
intmin - Smallest integer value.
flintmax - Largest consecutive integer in floating point format.
pi - 3.1415926535897....
i - Imaginary unit.
inf - Infinity.
nan - Not-a-Number.
isnan - True for Not-a-Number.
isinf - True for infinite elements.
isfinite - True for finite elements.
j - Imaginary unit.
true - True array.
false - False array.
Specialized matrices.
compan - Companion matrix.
gallery - Test matrices.
hadamard - Hadamard matrix.
hankel - Hankel matrix.
hilb - Hilbert matrix.
invhilb - Inverse Hilbert matrix.
magic - Magic square.
pascal - Pascal matrix.
rosser - Classic symmetric eigenvalue test problem.
toeplitz - Toeplitz matrix.
vander - Vandermonde matrix.
wilkinson - Wilkinson's eigenvalue test matrix.
>>
which is a MOST EXCELLENT way to get an overview of what is available--you can then go read the detailed doc for any that look like might be of some interest/use.
There's no way in the doc to get the same list in one place--it takes clicking on sublink after sublink after sublink to work ones way down the tree.
Appreciated !
lemme just verify about something , if I have rep with different values, not actually 1 2 3 4 5 6 , for example like rep=[4 2 1 5]; so in order to get the correct output as I explained in my comment above I do this:
y=[1 -2 3 -2]; repl=[4 2 1 5];
>> repelem(y>0,repl)
right?
also could you please explain to me what does that mean when you input y>0 to repelem (I know what repelem does) but Im not familar with syntaxt y>0 as input to function, we familar for instance to input number/arrray/string .. but not logical conditions!
thanks if you could clarify the input y>0 to function repelem in other words what does it mean
"clarify the input y>0 to function"
I'd suggest you'll learn far more more quickly and retain better if you just try it and see... :)
Then look for "logical indexing" in the documentation to read all about it.
One of most powerful of the MATLAB idioms...
And, btw, note that using the result of a function or an expression as above is functionally no different than having written
tmp=y>0;
repelelm(tmp,repl)
without creating the temporary variable--MATLAB does the requested operation and saves the result behind the scenes until it is no longer needed but doesn't then leave anything in the workspace once the function that used it finishes.
Of course, if one needs the same result more than once, it'll generally be more efficient to make the temporary rather than recompute the same thing two or more times...

Sign in to comment.

Tags

Asked:

on 15 Jul 2020

Commented:

dpb
on 16 Jul 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!