Creating new vector and using IF correct or loop?

1 visualización (últimos 30 días)
Daniel
Daniel el 25 de Sept. de 2014
Editada: Andrei Bobrov el 25 de Sept. de 2014
Hello,
I need some help with my matlab code.
I have two vectors of data: "position" and "time", The position vector has a maximum value of ~2 and a minimum value of ~-1. Booth vector are of 1x500000 points. My time vector looks like this: time=(0:length(position)-d)*dt %dt is the value between each point (2e-4), delta time
I want to be able to construct a new vector called "force".
This vector should be created by multiplication the position vector with a certain value. But I have different values for this multiplication. If the current value of the position is lower than 0.5, it should be multiplied with a certain value, x If it is between 0.5 and 1.5, it should be multiplied with a value, y. And so on...
I should then also be able to plot "time" vs "force".
I guess I should use some if and/or elseif commands. But how do I get it right?
Thank you.
  1 comentario
Image Analyst
Image Analyst el 25 de Sept. de 2014
Make it easy for people to help you by attaching a data file and a snippet of code to read it in to some variables.

Iniciar sesión para comentar.

Respuesta aceptada

Adam
Adam el 25 de Sept. de 2014
Just put together a multiplication vector as e.g.
multVec = zeros( size( position ) );
multVec( position < 0.5 ) = m1;
multVec( 0.5 <= position & position < 1.5 ) = m2;
...
Then:
force = position .* multVec;

Más respuestas (1)

Andrei Bobrov
Andrei Bobrov el 25 de Sept. de 2014
Editada: Andrei Bobrov el 25 de Sept. de 2014
v = [-inf,.5,1.5,inf];
[~,ii] = histc(position,v);
m = [2 8 15]'; % example as m = [m1,m2,m3];
force = m(ii).*position;

Categorías

Más información sobre Multidimensional Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by