Median(here it's middle ) of vector/array of every two adjacent values in array (value(i+1)-value(i)).
Show older comments
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)
dpb
on 15 Jul 2020
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
Mohamed Jamal
on 15 Jul 2020
dpb
on 15 Jul 2020
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.
Mohamed Jamal
on 15 Jul 2020
dpb
on 15 Jul 2020
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.
dpb
on 15 Jul 2020
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.
Mohamed Jamal
on 15 Jul 2020
dpb
on 15 Jul 2020
"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...
dpb
on 16 Jul 2020
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...
Categories
Find more on Matrix Indexing 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!