for loop to remove corresponding vector values

3 次查看(过去 30 天)
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 个评论
Adam
Adam 2018-10-15
编辑:Adam 2018-10-15
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 2018-10-16
Thank you Adam, that was very helpful and I managed to do it with your tips.

请先登录,再进行评论。

回答(1 个)

madhan ravi
madhan ravi 2018-10-15
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.

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by