for loop to remove corresponding vector values

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 个评论

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?
Thank you Adam, that was very helpful and I managed to do it with your tips.

请先登录,再进行评论。

回答(1 个)

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 个评论

ignore the y and q part please, it is just an example. The real code is below.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

提问:

2018-10-15

评论:

2018-10-16

Community Treasure Hunt

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

Start Hunting!

Translated by