How to do simple Excel functions using Matlab instead

Hi all,
This may be fairly easy and I apologize in advance as I am relatvely new to coding. I have have a 27 by 3 variable which contains velocity data. I want to get acceleration by dividing the change in vleocity by the chnage in time, which I have as another varibale (change in time). I know how to do this rather easily uisng Excel but I have 100's of files that I am running through Matlab so it would be much easier to add to to my Matlab code. Basicly, I want to take (2,1) from the velocity data subtract it from (1,1) and then divide it by another variable. I then want this to be repeated for all 27 in the velocity data (eg. next subtract(3,1) form (2,1) then divide by same varaible). I also want to do this for the other two rows in the same manner. What is the most efficent way to do this.
Thanks for the help!

 Respuesta aceptada

Star Strider
Star Strider el 7 de Nov. de 2019
Since you actually want to take the numerical derivative (rather than calculate the differences between elements of your vectors as with the diff function), use the gradient function. (It would likely be easier to use it on each column separately, then do element-wise division.) You will likely need to experiment with it.

4 comentarios

Thank you for your advice. I had a quick question. If I have time as a 27 by 1 column and velocity as a 27 by 1 column, would it be possible to use the gradient function to get the derivaties of those two data sets. And if you wouldnt mind could you show me an example of how to code it. I really apperciate the help!
My pleasure!
Here is an example of something similar to your data (if I understand correctly):
time = linspace(0, 9, 27).'; % Create Time Vector & Transpose
velocity = exp(time*[-0.1 -0.2 -0.3]); % Create Velocity Matrix & Transpose
[~,dvc] = gradient(velocity); % Gradient Down Columns
dvcdt = dvc ./ gradient(time); % Numerical Derivative With Respect To Time
figure
plot(time, velocity)
grid
title('Original Data')
figure
plot(time, dvcdt)
grid
title('Time Derivatives')
The data do not need to be evenly-sampled with respect to time, although that would be preferable. Also note that derivatives will amplify noise (my example is noise-free), so you may need to filter your data (likely with a lowpass filter) to eliminate as much noise as possible. Broadband noise is almost impossible to eliminate with a frequency-selective filter however, so other approaches (such as using wavelet decomposition) may be necessary in that event.
Thank you again, I was able to use the information you provided to get it to work perfectly!!
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (1)

Hi Kevin,
You should be able to access elements on the array using a proccess known as indexing.
% define array x as 3x3
% We can access each element with a process know as indexing
x = [1, 2, 3; 4, 5, 6; 7, 8, 9]
% To access the middle value we want the second row and second column, Note
% matlab starts inxexing at 1 rather than 0.
% x(rows, coloums)
x(2,2)
% Or you can access the correct element with
x(5) % 1,2,3,4,5. 5 if the fifth element in the matrix
% I'm not exactly sure what you want from the question but wrapping;
vel1(2,1)/ vel2(1,1)
% in a for loop and incrementing as required might might solve your problem?
Hope that helps,
Christopher

Categorías

Etiquetas

Preguntada:

el 7 de Nov. de 2019

Comentada:

el 7 de Nov. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by