for loop to remove corresponding vector values

3 visualizaciones (últimos 30 días)
Madlab
Madlab el 15 de Oct. de 2018
Comentada: madhan ravi el 16 de Oct. de 2018
I need to edit the vector in 2 variables such that they correspond to each other.
For example,
y = 1 2 3 4 5 6
q = 8 26 1 5 1 6
Let's say I fix q and shift y by 1
y = 1 2 3 4 5 6
q = 8 26 1 5 1 6
I need vector y1 = 1 2 3 4 5 and q1 = 26 1 5 1 6
This means I need to kick out 6 in y and 8 in q respectively.
Let's say I fix q and shift y by 2
y = 1 2 3 4 5 6
q = 8 26 1 5 1 6
As before, now I need to remove 5,6 in y and 8,26 in q respectively.
I want to do this in a for loop as my vector is very long. Right now, I am struggling to get the vector right for q (which is my soundtwo) as shown below. Any tips?
% Creating time vector, "t"
t = linspace(0,16*pi,1000);
sound1 = 5*(cos(t) + 1*(rand(size(t))-0.5));
sound2 = 8*(cos(t) + 1.5*(rand(size(t))-0.5));
% Setting the time shift "dt"
dt = 1000;
% Creating a matrix to store product later on
list = zeros(dt,1);
% For loop for different shifts
for i=1:dt
% Now edit sound1 such that sound1 shifts while sound2 remains unchanged
%different time shift
sound1 = 5*(cos(t+ i ) + 1*(rand(size(t))-0.5));
sound2 = 8*(cos(t) + 1.5*(rand(size(t))-0.5));
% Shifting sound1
soundone = sound1(i:numel(sound1))
% Sound 2 unchanged, but have to assign respective vector to sound1
soundtwo = sound2()
multipliedsound = (soundone) .* (soundtwo);
add = sum(multipliedsound)
product = add / numel(t);
% Append product to list vector
list(i,1) = product;
end
  2 comentarios
Adam
Adam el 15 de Oct. de 2018
Editada: Adam el 15 de Oct. de 2018
Just reverse ( flip ) your q vector and knock off the last 2, or however many you want to shift by, of both your y and q vectors. Then flip q again afterwards if the process stops at some point and you want your q vector back to normal.
I don't really understand how the code in the for loop relates to the example though. Where is the shift?
Madlab
Madlab el 16 de Oct. de 2018
Thank you Adam, that was very helpful and I managed to do it with your tips.

Iniciar sesión para comentar.

Respuestas (1)

madhan ravi
madhan ravi el 15 de Oct. de 2018
y = [1 2 3 4 5 6]
q = [8 26 1 5 1 6]
y(y==5 | y==6)=[]
q(q==8 | q==26)=[]
You don’t need loop to do this even if the vector is so long.
  2 comentarios
Madlab
Madlab el 15 de Oct. de 2018
ignore the y and q part please, it is just an example. The real code is below.
madhan ravi
madhan ravi el 16 de Oct. de 2018
Oh just saw your comment

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements 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